ASP.NET, Coding

Event Handling using Delegates

An event is an action or occurence such as mouse clicks, key presses, mouse movements or some system generated interrupts. In object oriented methodology events are messages sent by an object to indicate some state change. This is an effective mean of interprocess communication.

Event Handling with Delegates:

In C#, delegates are used for event handling. The .Net framework event model uses delegates to bind event notifications with methods known as ‘Event Handlers’. When an event is generated, the delegate calls the event handler. The delegates and the associated event handler both are declared in a class, which is also called the ‘Publisher’ class. This class basically fires the event i.e., an object of this class involes the event. There will be more than one subscriber class, which will accept the event and so athe event handler mehtod will be defined in the subscriber class.

Event Definition:

Let us consider a delegate called ‘ShiftEnd’ and an event called RingtheBell, which invokes the delegate. Following code declares both:

public delegate void ShiftEnd();
private event ShiftEnd RingtheBell;

Both these delegate and the invoking event could be part of a Factory class, which will be the publisher for these events of ringing of a bell to declare the end of one shift for the factory workers.

Subscribing to the Event:

Consider a worker class, which will accept the ‘RingtheBell’ event. So the event handler ‘LeaveforHome’ will be defined in the Worker class i.e., the Worker class will have a method named ‘LeaveforHome’ which will accept the event. So the Worker class is one of the subscribers, which will accept the event.

Worker w = new Worker();
RingtheBell = new ShiftEnd(w. LeaveforHome);

Please note, the event RingtheBell takes care of the LeaveforHome method. Write this block of code at athe place of event notification.

 if(RingtheBell != null)
         RingtheBell();

This invokes all associated delegates. So the LeaveforHome() method will be ultimately called. There could be more than one delegate associated. The above code snippet checks whether at least one subscriber is there; without this the code will throw an exception.

Passing Parameters to Event Handlers:

Subscribing method might need some input to be passed into it at runtime. The event class can have these input at runtime. Another class is to be defined for this purpose, which will enclose these input parameters as it’s private members and supply public accessor methods to retrieve the value. This particular class must be derived from System.EventArgs.
For example our LeaveforHome() method might need some timing information. So we create a TimeArgs class which can pass this information to the method at runtime via it’s accessor methods.

public class TimeArgs: EventArgs
{
 private int hr;
 private int mn;
 private int sec;
     public TimeArgs( int h, int m, int s)   //the constructor
 {
  this.hr = h;
  this.mn = m;
  this.sec = s;
 }
         // now the accessors
 public int gethour()
 {
  return hr;
 }
 public int getminute()
 {
  return mn;
 }
 public int getsecond()
 {
  return sec;
 }
}