Introduction An event is, essentially, an automatic notification that some action has occurred. Events work like this: An object that has an interest in an event registers an event handler for that event. When the event occurs, all registered handlers are called. Event handlers are represented by delegates. Events are members of a class and are declared using the event keyword.
Its general form is shown here:
event event-delegate object-name;
Here, event-delegate is the name of the delegate used to support the event, and object-name is the name of the specific event object being created.
Example
Let’s begin with a very simple example:
// A very simple event demonstration. using System;
// Declare a delegate for an event delegate void MyEventHandler();
// Declare an event class. class MyEvent { public event MyEventHandler activate; // this is called to fire the event. public void fire() { if(activate != null) activate(); } }
class EventDemo { static void handler() { Console.WriteLine("Event occurred"); }
public static void Main() { MyEvent evt = new MyEvent(); // add handler() to the event list evt.activate += new MyEventHandler(handler); // fire the event evt.fire(); } }
Description of above example N Although simple, this program contains all the elements essential to proper event handling. Let’s look at it carefully. The program begins by declaring a delegate for the event handler, as shown here:
delegate void MyEventHandler();
All events are activated through a delegate. Thus, the event delegate defines the signature for the event. In this case, there are no parameters, but event parameters are allowed. Because events are commonly multicast, an event should return void.
Next, an event class, called MyEvent, is created. Inside the class, an event object called activate is declared, using this line:
public event MyEventHandler activate;
Notice the syntax. This is the way that all types of events are declared. Also declared inside MyEvent is the method fire( ), which is the method that a program will call to signal (or, “fire”) an event. It calls an event handler, through the activate delegate, as shown here:
if(activate != null) activate();
Notice that a handler is called if and only if activate is not null. Since other parts of your program must register an interest in an event in order to receive event notifications, it is possible that fire( ) could be called before any event handler has been registered. To prevent calling a null object, the event delegate must be tested to ensure that it is not null.
Inside EventDemo, an event handler called handler( ) is created. In this simple example, the event handler simply displays a message, but other handlers could perform more meaningful actions. In Main( ), a MyEvent object is created and handler( ) is registered as a handler for this event, as shown here:
MyEvent evt = new MyEvent(); // add handler() to the event list evt.activate += new MyEventHandler(handler);
Notice that the handler is added using the += operator. Events support only += and – =. Finally, the event is fired as shown here:
// fire the event evt.fire();
Calling fire( ) causes all registered event handlers to be called. In this case, there is only one registered handler, but there could be more, as the next section explains.
Multicast Events
Events can be multicast. This enables multiple objects to respond to an event notification. Here is an event multicast example:
// An event multicast demonstration. using System;
// Declare a delegate for an event. delegate void MyEventHandler(); // Declare an event class.
class MyEvent { public event MyEventHandler activate;
// this is called to fire the event. public void fire() { if(activate != null) activate(); } }
class X { public void Xhandler() { Console.WriteLine("Event received by X object"); } }
class Y { public void Yhandler() { Console.WriteLine("Event received by Y object"); } }
class EventDemo { static void handler() { Console.WriteLine("Event received by EventDemo"); } public static void Main() { MyEvent evt = new MyEvent(); X xOb = new X(); Y yOb = new Y(); // add handler() to the event list evt.activate += new MyEventHandler(handler); evt.activate += new MyEventHandler(xOb.Xhandler); evt.activate += new MyEventHandler(yOb.Yhandler);
// fire the event evt.fire(); Console.WriteLine();
// remove a handler evt.activate -= new MyEventHandler(xOb.Xhandler); evt.fire(); } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|