How to provide event handling in a class hierarchy in Visual C# (319296)



The information in this article applies to:

  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2002)

This article was previously published under Q319296

SUMMARY

Events are one of the most frequently used object-oriented programming techniques in the .NET Framework. This article defines what an event is and how you can implement events in one of the .NET programming languages, C#.

back to the top

What is An Event?

An event is an action that you can respond to or handle in code. An event is raised by an object, and you can supply code that acts on events. The process of supplying code that is executed when an event is raised is known as "subscribing to an event", which is done in the form of an event handler.

An event may have many handlers subscribed to it, and these handlers are called when the event is raised. An event handler function must match the signature that the event requires. This signature is part of the definition of an event and is specified by a delegate.

back to the top

What Are Delegates?

Delegates are:
  • Classes that are frequently used in the .NET Framework to build event-handling mechanisms.
  • Object-oriented, type-safe, and secure.
A delegate is made up of:
  • A reference to an object.
  • References to one or more methods in the object.
The event model uses delegates to bind events to the methods that are used to handle the events. With delegates, other classes can register for event notification by specifying a handler method. When the event occurs, the delegate calls the bound method.

back to the top

Implement Event Handling

To handle an event, you must subscribe to the event by providing an event handler function whose signature matches that of the delegate that is specified for use with the event.

To implement event handling, follow these steps:
  1. Start Visual Studio .NET or Visual Studio 2005, and create a new Visual C# .NET Console Application project named DerEvt.
  2. On the Project menu, click Add Class.
  3. In the Add New Item dialog box, type EvtSource.cs in the Name box, and then click Open.
  4. Add a delegate named MyEvtHandler before the class declaration:
    public delegate void MyEvtHandler( object sender, System.EventArgs e );
  5. Add a function named FireMyEvent to the class after the class constructor. Make a call to another function named MyEvt in the implementation for this class. MyEvt function is of type MyEvtHandler. The code appears as follows:
    public void FireMyEvt ( object sender, System.EventArgs e )
    {
    	MyEvt ( sender, e );
    }
    
    public MyEvtHandler MyEvt;
    					
  6. Add a class EvtRcvBase to the main program outside the definition of Class1 that was created by the wizard. For the newly created class, follow these steps:
    1. Create a constructor for the EvtRcvBase class.
    2. Create an instance of the EvtSource class that was created in step 3 and name it evtSrc.
    3. Create a virtual function named OnMyEvt in the class definition for EvtRcvBase, and then add a WRITELINE statement to its implementation.
    4. Add the subscription code for the event MyEvt provided by the EvtSource class.
      This code hooks the handler MyEvtHandler to the event MyEvt. To do this, use the += operator to add a handler to the event in the form of a new delegate instance that is initialized with your event handler method.
    5. Add another method named SomeMethod to the EvtRcvBase class and call the FireMyEvt method on the evtSrc object. The completed code appears as follows:
    class EvtRcvBase 
    {
    	protected EvtSource evtSrc;
    
    	public EvtRcvBase ( )
    	{
    		evtSrc = new EvtSource ( );
    		evtSrc.MyEvt += new MyEvtHandler ( OnMyEvt );
    	}
    
    	public virtual void OnMyEvt ( object sender, System.EventArgs e )
    	{
    		Console.WriteLine ( "Base method called" );
    	}
    
    	public void SomeMethod ( )
    	{
    		evtSrc.FireMyEvt ( this, null );
    	}
    }
    					
  7. Add another class EvtRcvDer derived from EvtRecvBase outside the main function. Override the OnMyEvt method implemented in EvtRcvBase.
    class EvtRcvDer : EvtRcvBase
    {
    	public override void OnMyEvt ( object sender, System.EventArgs e )
    	{
    		Console.WriteLine ( "Derived method called" );
    	}
    }
    					
  8. Create an instance m of the EvtRcvBase class in the main function. Call the method named SomeMethod on m. The main method appears as follows:
    static void Main(string[] args)
    {
    	EvtRcvBase m = new EvtRcvDer ( );
    	m.SomeMethod ( );
    }
    					
  9. Build and run the application.

Modification Type:MajorLast Reviewed:1/18/2006
Keywords:kbHOWTOmaster kbInheritance KB319296 kbAudDeveloper