A small and helpful class that I find myself adding to every single project I start these day is ”GenericEventArgs”.

Microsoft included the System.EvenHandler<T> class in .NET, taking away the need to create custom delegates to enable the use of custom event arguments . This class takes it one step further. It’s a simple generic class with the signature EventArgs<T>, deriving from System.EventArgs. It accepts a single instance of T thru the constructor which it exposes via a property “Value” of the type T.

Now where I would earlier create custom eventarg classes to pass a single argument with an event, I simply use this generic implementation.

Before you read any further, please notice that I did not come up with this my self – I first saw it on a web page (probably some guys blog – if so, this is to you: I apologize a thousand times for my corrupted memory) – of which I don’t remember the name or the address and was not able to locate thru the usual goggling.And now to the point. The class in all its glory looks like this:

public class EventArgs<T> : EventArgs
   private T value;
   public T Value
      get { return this.value; }

   public EventArgs(T value)
      this.value = value;

The class could easily be extended to sport more than one type / instance / property (although I am still struggling with the naming – somehow “Value2” doesn’t sound that good). Below is a small example of how things look when put to use.

The event definition:

public event EventHandler<EventArgs<int>>

protected void OnCalculationCompleted(int result)
    if (this.CalculationCompleted != null)
new EventArgs<int>(result));

The event hookup:

this.CalculationCompleted += new

The event handler:

private void Calculator_CalculationCompleted(
object sender, EventArgs<int> e)
    lblResult.Text = e.Value.ToString();

(Note that I added “private” to the event handler method my self – beats me why this is not done by default – maybe someone can explain).

Although the event argument signature is left somewhat anonymous I find that doing things with the GenericEventArgs class greatly improve my development speed while keeping the number of small support class down. Also the lack of inherent meaning can be easily made up for by applying xml comments to be displayed by IntelliSense (you guessed it – Visual Studio is my weapon of choice). 

I found no real reason to upload any source for this example. Simply copy/paste the code above into a freshly created class and Bob’s your uncle.

 Lastly the code examples in this post have some weird line breaks – please try to ignore them. I realy had to struggle with the editor here at wordpress to get it to format somewhat correctly.

If you find this post usefull, comments and suggestions are much appriciated.


Tags: , , , , , ,

Leave a Reply

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

You are commenting using your 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: