This post originated from an RSS feed registered with .NET Buzz
by Roy Osherove.
Original Post: Manage many events easily with EventHandlerList
Feed Title: ISerializable
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/rosherove/Rss.aspx
Feed Description: Roy Osherove's persistent thoughts
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:
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.