Tuesday, July 25, 2006

.Net Events made easy

There is one thing that is always difficult to understand for C# beginners (and even more experience developers): .Net events. Many persons keep asking me questions about events handling.

What is the delegate?
Where/How do I declare my event?
etc...

First reflex is to look for a good article or some documents that will make it crystal clear....but sincerely I was not able to find a good one, or let say a good and not too long one.

What is an event?
An event allows an element (page, control, winform) to send a message that can be caught or not by other elements.

How does it work?
One element defines and exposes an event. This is the sender or the provider. Other elements can then subscribe to this event and do something when they receive it. They are the receivers.

What does it look like in code?
First we will declare the event in the sender (page, control, etc..) To declare an event we will need to define the event, its delegate and the method to raise it. We will also declare a class for passing data along with the event but it is not required.

To declare an event, you will need to define a delegate. A delegate simply defines the definition of the method, which will receive the event (this is the handler)

public delegate void MyEventHandler(object sender, MyEventArgs e);

In the example above, the handler will receive the sender as an object and the arguments of the event of type MyEventArgs (If you don't need to pass arguments through your event just use the default System.EventArgs)

Then you declare your event as shown below (note that the event name "MyEvent" is preceeded with the handler defined previously)

public event MyEventHandler MyEvent;

Finally, declare the method that will raise your event. This is the method you will call to raise the event.

protected void OnMyEvent(MyEventArgs e)
{
if(MyEvent!= null)
{
MyEvent(this, e);
}
}

When you want to raise your event, just write

OnMyEvent( new MyEventArgs(param1, param2) );

Below is the code for the event's arguments class

public class MyEventArgs : EventArgs
{
private string param1= "";
private string param2= "";

public MyEventArgs (string param1, string param2)
{
this.param1= param1;
this.param2= param2;
}

public string Param1
{
get { return param1; }
}

public string Param2
{
get { return param2; }
}

}

Now, let's see how that works on the receiver. We will take a ASP.Net Web Form for this purpose. You will need to declare that the web form should subscribe to the event.
In the OnInit method, you will instantiate the handler passing the method that will handle the event and attach the handler to the event.

override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
MyControl.MyEvent += new MyControl.MyEventHandler(MyControl_MyEvent);
base.OnInit(e);
}

The method definition that will handle the event is the one we defined for the delegate. Once you are in the body of the method, you can easily access the parameters sent throught the event arguments.

private void MyControl_MyEvent(object sender, MyControl.MyEventArgs e)
{
string param1 = e.Param1;
string param2 = e.Param2;
//Do your stuff

}

When should I use events?
Events are the basis of programmation in most plaform today. When you click a button, it raises a "onclick" event that you can decide to catch or not.Just think the same way for the blocks you build...

For instance, a block is creating an order; it can raises the event OrderCreated for other blocks to catch it and do whatever is necessary (send a message to a queue, refresh the screen, etc...)
Events are really a powerful way to expose data without being tied to the other blocks that will receive the events.

Well that's it! I hope you find this post useful.