Home > Programming > Generic EventArgs

Generic EventArgs

It’s around the web, and it’s just too simple to avoid.

I’m talking of course about a generic EventArgs. When you have an event you need to fire and it should carry some argument(s), but you don’t want to commit to a full implementation of a custom EventArgs, you can use this handy technique to make a generic version of EventArgs:

Here is my version of it (for posterity and such):

public class EventArgs<T> : EventArgs
{
    public EventArgs(T value)
    {
        Value = value;
    }
    public T Value { get; private set; }
    public new static readonly EventArgs<T> Empty = 
        new EventArgs<T>(default(T));
}

That’s about as short as classes get! You should take the time to document it to give it a seamless, .NET-looking documentation when it comes up in Intellisense.

In the class that will use the event, we first need an event handler like this:

public event EventHandler<EventArgs<T>> MyEvent;

I put a T up there because at that point T can be any available type (from the mighty power of generics!), such as int, string, or a class. I use it mostly for text messages for certain events:

public event EventHandler<EventArgs<string>> MessageSent;

Next, prepare to invoke it. You don’t want to Invoke that by hand all the time, so use a function to cover that up:

private void InvokeMessageSent(string message)
{
    var handler = MessageSent;
    if (handler != null) handler(this, new EventArgs<string>(message));
}

And you’re done! Next time you want to fire that event, just do this:

InvokeMessageSent("Warning, I'm sending a message!");

The class which listens for this event will need to register that event to be ready to handle it:

var MyObject = new MyClass();
MyObject.MessageSent += new EventHandler<EventArgs<string>>(MessageSent);

Or simpler with implicit typing:

MyObject.MessageSent += MessageSent;

When should this be used?

First of all, you only ever need a generic EventArgs if your event is going to carry some required information that’s not already in the sender object. It can happen that the event is sent from a static class, so in that case, make the sender null and you only have an EventArgs to put your information. Use EventArgs when you have a few types of arguments that need to be sent in various cases.  (For example, I was about to create a StringEventArgs and IntegerEventArgs when I thought “There must be a simpler way to do this”).

Why would I need events in the first place?

I could make a post on this alone. Events are a simplified means of telling “the world” about things happening. It’s up to “the world” to either listen to the event or not. If you have a processor object (or static class) that has different statuses it can announce, you can use an event to announce a change. A form calls a process and “listens” to the various events to update a progress bar or status label. It’s really all about separating the View from the Controller.

Advertisements
Categories: Programming Tags:
  1. stam
    2011-09-13 at 1:36 am

    you missed one line of code in your EventArgs class:
    public new static readonly EventArgs Empty = new EventArgs(default(T));

    • MPelletier
      2011-09-13 at 4:26 am

      Oh, good catch! Thanks! WordPress swallows the <T>’s, so here’s that line in all its glory:
      public new static readonly EventArgs<T> Empty = new EventArgs<T>(default(T));

  1. No trackbacks yet.

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: