Writing custom Events using Delegates


In many situations we need to write custom events to notify occurrences of some event in our class. This article describes how to write custom events in a class using delegates and the subscription of same events in another class.Usage of custom event arguments is also described

In many situations we need to write custom events to notify occurrences of some event in our class to other classes which are using our class.
Following steps describes how to write custom events.

1. Declare a delegate. Choose the signature of the delegate according to
the return type and input parameter.



/// the custom delegate must be available to both
/// publisher and listener. this is because, event is
/// in publisher class is of type of this delegate.
/// also the the subscriber needs this information to
/// subscribe the event.
public delegate void delEvent(CustomEventArgs cev);



2. In the publisher class,declare an event. event's type will be the delegate declared above.
In any method from publisher class, this event can be raised. Event can be raised just by calling the event from the Publisher class and passing required parameters.



///The "Publisher" class
///this is the class which holds some data
///and whenever somebody request that information
///an event is raised.
public class Information
{
public event delEvent evInformationEvent;

public Information()
{

}

public void GetData()
{
///raising the event. this event will be available to
///all listeners or subscribers who have subscribed to the
///event of this class.

CustomEventArgs cev = new CustomEventArgs();
cev.Data1 = "message1 from publisher";
cev.Data2 = "message2 from publisher";

evInformationEvent(cev);
}

}




3. In the listener class, subscribe to the event of the publisher class.
Add event handler in the listener class.



/// the "Listener" class
public partial class Form1 : Form
{
Information inf;

public Form1()
{
InitializeComponent();
inf = new Information();
///subscribe to the event of Information class. whenever
///event is raised, the method "inf_evInformationEvent" executes.
inf.evInformationEvent += new delEvent(inf_evInformationEvent);
}

private void button1_Click(object sender, EventArgs e)
{
///calling the GetData() method of information class.
inf.GetData();
}

/// this is the event-handler. this method will handle the
/// event whenever it is raised.
private void inf_evInformationEvent(CustomEventArgs cev)
{
///the "cev" class is passed by the event from the publisher class.
MessageBox.Show(cev.Data1);
MessageBox.Show(cev.Data2);

}
}



4. whenever the event is raised in Publisher class, the event handler in Listener class will get executed.


/// this is the event-handler. this method will handle the
/// event whenever it is raised.
private void inf_evInformationEvent(CustomEventArgs cev)
{
///the "cev" class is passed by the event from the publisher class.
MessageBox.Show(cev.Data1);
MessageBox.Show(cev.Data2);

}


5. The event arguments can be simple data types or we can create custom class to add more information. Using class derived from "EventArgs" class, the built-in delegates of .net framework can be used for declaring custom events. the argument type used here is below



///this is a custom class which is used as event argument.
///the information which needed to pass from the publisher to
///listener can be stored in this class.
public class CustomEventArgs : EventArgs
{
private string data1;
private string data2;

public string Data1
{
get
{
return data1;
}
set
{
data1 = value;
}
}

public string Data2
{
get
{
return data2;
}
set
{
data2 = value;
}
}
}



Attachments

  • Source Code (44612-53953-Form1.cs.txt)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: