.Net 2.0 INotifyPropertyChanged Interface Implementation...help needed..urgent

H

Hardeek Thakkar

Dear friends,

In my current project I am using the CustomCollections with the help of
BindingList<T> generic class to store the database records instead using
DataSet objects as offline database identities.

Now I do have a large number of property classes each having a large
number of properties in it.

I want to implement the INotifyPropertyChanged interface in every
property class as follows:

The purpose to implement the INotifyPropertyChanged interface is I want
to inform the UI whenever any property change occurs and update the it
accodingly. Also I need to inform the ListChanged event of the my
BindingList<T> collection object using the PropertyChangedEvent of
INotifyPropertyChanged interface. I know that the PropertyChangedEvent
automatically raises the ListChanged event of the BindingList<T> object.

I want to maintain a flag whose value will be false whenever the
collection gets changed means any property value of any item in the
collection gets changed.

My code is like below:


public class Customer :INotifyPropertyChanged
{

private int number;

public int Number
{
get { return number; }
set { number = value;if(value!=number)
OnPropertyChanged("Number"); }
}

private string name;

public string Name
{
get { return name; }
set { name = value;if(value != name)
OnPropertyChanged("Name"); }
}

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
}

Now what if my class has 100 properties then....

Is there any way out so I dont need to write the same code like below
again and again in each property's set method:

if(value != name) OnPropertyChanged("Name");

I dont want to write the above line for every property.

So is it possible to do this using Reflection and subscribe for the
event PropertyChanged for every property in the class in a single code
block..

I have seen such a code months ago on any website but can't recall it
right now as that time I was not so serious and not knowing I might have
to implement this in future:)

Suggest me how to do this and put the code snippet if possible.

THANKS in ADVANCE.

Regards,
Hardeek Thakkar
 
J

Joanna Carter [TeamB]

"Hardeek Thakkar" <[email protected]> a écrit dans le message de
news: (e-mail address removed)...

| I want to implement the INotifyPropertyChanged interface in every
| property class as follows:

Create a base class with a generic property setter method and call that from
the separate property setters.

public class BaseObject : INotifyPropertyChanged
{
protected void SetValue<valueT>(string fieldName, valueT value)
{
FieldInfo fi = GetType().GetField(fieldName, ...);
fi.SetValue(value);
// call propertychanged event with fieldName
}


}

public class Customer : BaseObject
{
private string name;

public string Name
{
get {...}
set { SetValue<string>(value); }
}
}

Joanna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top