There are two approaches to implement events in .NET. The first one by using delegate keyword and the second by using EventHandler keyword
We will start with the first approach:
This approach is used when we want to pass the event's source object as parameter(sender)..
Example: ((EventDelegate ) sender).Add("one","Greg")
We have three classes.:EventDelegate ,Listener and Test Class.
namespase a{
public delegate void ChangedEventHandler(Object sender, EventArgs e);
public class EventDelegate : SortedList {
// An event that clients can use to be notified whenever the
// elements of the list change.
public event ChangedEventHandler Changed;
// Invoke the Changed event; called whenever list changes
protected virtual void OnChanged(EventArgs e)
{
if (Changed != null)
Changed(this, e);
}
// Override some of the methods that can change the list;
// invoke event after each
public override int Add(object key,object value) {
int i = base.Add(key,value);
OnChanged(EventArgs.Empty);
return i;
}
public override void Clear()
{
base.Clear();
OnChanged(EventArgs.Empty);
}
public override object this[Object key]
{
set
{
base[key] = value;
OnChanged(EventArgs.Empty);
}
}
}
}
namespace Test{
class Listener { private EventDelegate List; public Listener(EventDelegate list) { List = list; // Add "ListChanged" to the Changed event on "List". List.Changed += new ChangedEventHandler(ListChanged); } // This will be called whenever the list changes. private void ListChanged(object sender, EventArgs e) { Console.WriteLine("This is called when the event fires."); } public void Detach() { // Detach the event and delete the list List.Changed -= new ChangedEventHandler(ListChanged); List = null; } }using a;class Test { // Test the EventDelegate class. public static void Main() { // Create a new list. EventDelegate list = new EventDelegate (); // Create a class that listens to the list's change event. Listener listener = new Listener(list); // Add and remove items from the list. list.Add("one","item 1"); list.Clear(); listener.Detach(); } }In the second approach we wiil also use three classes:EventWithHandler,Listener,and Test.As you can see only the class EventWithHandler is changed.namespase a{// A class that works just like SortedList, but sends event // notifications whenever the list changes: public class EventWithHandler: SortedList { // An event that clients can use to be notified whenever the // elements of the list change: public event EventHandler Changed; // Invoke the Changed event; called whenever list changes: protected virtual void OnChanged(EventArgs e) { if (Changed != null) Changed(this,e); } // Override some of the methods that can change the list; // invoke event after each: public override int Add(object key,object value){ int i = base.Add(key,value); OnChanged(EventArgs.Empty); return i; } public override void Clear() { base.Clear(); OnChanged(EventArgs.Empty); } public override object this[Object key] { set { base[key] = value; OnChanged(EventArgs.Empty); } } }}Very ineteresting article related to enent driven programming
No comments:
Post a Comment