Generic Change Event handler
-
Lets say I'm using the vs2008 convention of setting up properties and have a Base class and inherited classes. Is there a way in my base class that I can attach event handler to properties the child and base may have to determine if the set method of a property get used. Looking for something to add to base class that would setup the notification. eg static enum State (isUnchanged,isNew,isModified,isDeleted) class MyBase { public State myState; } class MyChild : MyBase { public string FirstName {get;set;} public string LastName {get;set;} }
-
Lets say I'm using the vs2008 convention of setting up properties and have a Base class and inherited classes. Is there a way in my base class that I can attach event handler to properties the child and base may have to determine if the set method of a property get used. Looking for something to add to base class that would setup the notification. eg static enum State (isUnchanged,isNew,isModified,isDeleted) class MyBase { public State myState; } class MyChild : MyBase { public string FirstName {get;set;} public string LastName {get;set;} }
You can do something like this but you'll have to set the state in the setter for each property.
class MyBase { public event EventHandler StateChanged; private State state; public State State { get { return state; } set { if (state != value) { state = value; OnStateChanged(EventArgs.Empty); } } } protected virtual void OnStateChanged(EventArgs e) { EventHandler eh = StateChanged; if (eh != null) eh(this, e); } }
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
You can do something like this but you'll have to set the state in the setter for each property.
class MyBase { public event EventHandler StateChanged; private State state; public State State { get { return state; } set { if (state != value) { state = value; OnStateChanged(EventArgs.Empty); } } } protected virtual void OnStateChanged(EventArgs e) { EventHandler eh = StateChanged; if (eh != null) eh(this, e); } }
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)Not quite where I was heading, My baseclass will have no idea what properties the child class has so in pseduo code I'm wondering if something like this is possible The idea is to have a means of setting BaseClass protected void SetupHandlers() foreach property in MyProperties AttachSetChangeEvent () public BaseClass() { SetupHandlers(); }
-
Lets say I'm using the vs2008 convention of setting up properties and have a Base class and inherited classes. Is there a way in my base class that I can attach event handler to properties the child and base may have to determine if the set method of a property get used. Looking for something to add to base class that would setup the notification. eg static enum State (isUnchanged,isNew,isModified,isDeleted) class MyBase { public State myState; } class MyChild : MyBase { public string FirstName {get;set;} public string LastName {get;set;} }
I think another way to phrase this question is How about a way to override the set; method for a property In VS2008 you don't need to specify a private field for properties and can just use public int myprop {get;set;} So if I could figure out how to override that set method, maybe thats where I could add change event handler.
-
Not quite where I was heading, My baseclass will have no idea what properties the child class has so in pseduo code I'm wondering if something like this is possible The idea is to have a means of setting BaseClass protected void SetupHandlers() foreach property in MyProperties AttachSetChangeEvent () public BaseClass() { SetupHandlers(); }
I don't think it's possible. Maybe one of the other guys knows a way - if there is one it could be very interesting.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Not quite where I was heading, My baseclass will have no idea what properties the child class has so in pseduo code I'm wondering if something like this is possible The idea is to have a means of setting BaseClass protected void SetupHandlers() foreach property in MyProperties AttachSetChangeEvent () public BaseClass() { SetupHandlers(); }
Not without a lot of work, I'm afraid. With a class like
class NotifiableProperty<T> { T val; public event Action PropertyChanged; public T Value { get { return val; } set { val = value; PropertyChanged(); } } public static implicit operator T(NotifiableProperty<T> p) { return p.Value; } }
you can do
class MyClass
{
NotifiableProperty<int> p = new NotifiableProperty<int>();public MyClass() { p.PropertyChanged += new Action(() => Console.WriteLine("Changed")); } public int P { get { return p; } set { p.Value = value; } } }
and then when you do
MyClass m = new MyClass();
m.P = 20;you will see "Changed" printed on the screen. I don't know how to automatically hook to the events, and I don't think setting up events from the base class is a good idea, as there is the possibility that derived class members wouldn't have been initialized at that point. Maybe use reflection inside the derived class?
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro