Search The Blog
About this site

@RoyOsherove

Subscribe!

This site aims to connect all the dots of my online activities - from tools, books blogs and twitter accounts, to upcoming conferences, engagements and user group talks.

from 5whys.com
Twitter: @RoyOsherove
My Book: The Art of Unit Testing
Latest Posts
« The case for staged delivery and Agile methodologies | Main | [OT] Some free time coming soon »
Friday
Dec312004

Manage many events easily with EventHandlerList

There were many little snippets and corners during Juval's class that simply made my day every time. This is one of those.

What happens when you have a class that exposes many events (such as the "Button" or "Control" class that exposes "Click","Focus" etc..)? In the standard case you would have to have many delegates as members inside it and that would make your code look very messy. The Simpler soution however would be to use the EventHandlerList class that is found in the System.ComponentModel namespace. It's basically a hashtable of all the event handlers you want to keep (the delegates, actually) and allows you to use it for general event storing purposes. In fact, the Control class in the .Net framework use this internally to store its own events.

How do you use it? You implement the specific "add" and "remove" accessors for your events, and inside them, you redirect the added or removed delegate value into the EventHandlerList object. Like so:

private EventHandlerList m_handlers = new EventHandlerList();

           

public event EventHandler Saved

 {

   add

   {

    m_handlers.AddHandler("Saved",value);

   }

   remove

   {

    m_handlers.RemoveHandler("Saved",value);

   }

 }

 

 

 

 

Unfortunately, this cannot be done in VB.Net (you can't override event accessors in VB.Net!?).

Here's an implementation of a class that exposes 3 events, one of them is not a simple event handler type. You'll see that it's just as simple to exposes them, but only one member holds all the events.

 

      public class MyComplicatedClass

      {

            public delegate void SpecialEventDelegate(string someText,int someNumber);

 

            private EventHandlerList m_handlers = new EventHandlerList();

           

            public event EventHandler Saved

            {

                  add

                  {

                        m_handlers.AddHandler("Saved",value);

                  }

                  remove

                  {

                        m_handlers.RemoveHandler("Saved",value);

                  }

            }

           

            public event EventHandler Deleted

            {

                  add

                  {

                        m_handlers.AddHandler("Deleted",value);

                  }

                  remove

                  {

                        m_handlers.RemoveHandler("Deleted",value);

                  }

            }

            public event SpecialEventDelegate SpecialEvent

            {

                  add

                  {

                        m_handlers.AddHandler("SpecialEvent",value);

                  }

                  remove

                  {

                        m_handlers.RemoveHandler("SpecialEvent",value);

                  }

            }

           

            private void FireSavedEvent()

            {

                  EventHandler handler = (EventHandler)m_handlers["Saved"];

                  if(handler!=null)

                  {

                        handler(this,new EventArgs());

                  }

            }

           

            private void FireDeletedEvent()

            {

                  EventHandler handler = (EventHandler)m_handlers["Deleted"];

                  if(handler!=null)

                  {

                        handler(this,new EventArgs());

                  }

            }

           

            private void FireSpecialEvent()

            {

                  SpecialEventDelegate handler = (SpecialEventDelegate) m_handlers["SpecialEvent"];

                  int someNumber=3;

                  if(handler!=null)

                  {

                        handler("some text",someNumber);

                  }

            }

      }

 

 

PrintView Printer Friendly Version

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>