Tom Goff's .Net Musings

Tidbits of information regarding .Net, C#, and SQL Server.

COTW: EventHandler Generic Delegate

leave a comment »

The EventHandler Generic Delegate[^] allows developers to easily define events without having to define a custom delegate. In the following code, MyObject defines an event which uses a custom class derived from EventArgs[^]:

public class MyObject {
    // ... Other code ...

    /// <summary>
    /// This is a custom delegate for use with the
    /// <see cref="E:SomeEvent"/> event.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The event arguments.</param>
    public delegate void SomeEventHandler(Object sender,
        MyCustomEventArgs e);

    /// <summary>
    /// This event is fired when something happens.
    /// </summary>
    [field: NonSerialized]
    public event SomeEventHandler SomeEvent;

    /// <summary>
    /// This method is used to fire the <see cref="E:SomeEvent"/>
    /// event.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The event arguments.</param>
    protected virtual void OnSomeEvent(Object sender,
        MyCustomEventArgs e) {
        if (null != SomeEvent)
            SomeEvent(sender, e);
    }
}

public class MyCustomEventArgs : EventArgs {
    /// <summary>
    /// Initializes a new instance of the
    /// <see cref="MyCustomEventArgs"/> class.
    /// </summary>
    /// <param name="whatHappened">What happened.</param>
    public MyCustomEventArgs(String whatHappened) {
        this.whatHappened = whatHappened;
    }

    /// <summary>
    /// Holds a string that tells what happened.
    /// </summary>
    private String whatHappened;

    /// <summary>
    /// Gets a string that tells what happened.
    /// </summary>
    /// <value>What happened.</value>
    public String WhatHappened {
        get {
            return this.whatHappened;
        }
    }
}

This is a typical setup for events and is the only way to define events in .Net 1.x. However in .Net 2.0 the generic EventHandler can be used to forgo the definition of the delegate. In the following code, we have removed the custom delegate and replaced it with the generic EventHandler (the class MyCustomEventArgs is exluded from the code below because it is unchanged from the previous example):

public class MyObject {
    // ... Other code ...

    /// <summary>
    /// This event is fired when something happens.
    /// </summary>
    [field: NonSerialized]
    public event EventHandler<MyCustomEventArgs> SomeEvent;

    /// <summary>
    /// This method is used to fire the <see cref="E:SomeEvent"/>
    /// event.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The event arguments.</param>
    protected virtual void OnSomeEvent(Object sender,
        MyCustomEventArgs e) {
        if (null != SomeEvent)
            SomeEvent(sender, e);
    }
}

As you can see, the EventHandler generic delegate saves us from having to define custom delegates everytime we define a new custom EventArgs class.

Advertisements

Written by Tom

October 7, 2007 at 8:33 pm

Posted in .Net, C#, Class of the Week

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: