This post originated from an RSS feed registered with .NET Buzz
by Paul Vick.
Original Post: Custom events
Feed Title: Panopticon Central
Feed URL: /error.aspx?aspxerrorpath=/rss.aspx
Feed Description: a blog on Visual Basic, .NET and other stuff
In my previous entry on events (written well before even VS 2002 had shipped), I made the comment:
VB does not have a syntax for defining events that allows you to specify the field, the add method or the remove method. Those are always implicitly generated.
Now, most of the time this doesn't really matter. Most of the time, the code you write in the add and remove method is going to be the same boilerplate code over and over and over again, so you're going to want to just let the compiler do its thing and not worry about it too much. However, there are some situations in which you might want to take over managing an event's delegate. The most common case that I know of is the situation in which you have an object that raises a lot of events. For example, a Form can raise something like 85 different events. If you accept the default compiler behavior, this means that the compiler will generate a field for each and every event to store the event handlers for that event. Which means in the case of Form, that it would generate something like 85 fields, even though in most cases programmers only ever handle about 4-5 events on a Form!
One alternative to wasting all that space is to use a hashtable to store delegates for just the events that someone is handling. To do this, though, you need to be able to control what happens when someone hooks up to or unhooks from an event. So, in VB 2005, we're introducing something we call custom events that look something like this:
Class C1 Public Custom Event MyEvent As EventHandler AddHandler(ByVal d As EventHandler) ... End AddHandler
RemoveHandler(ByVal d As EventHandler) ... End RemoveHandler
RaiseEvent(ByVal o As Sender, ByVal e As EventArgs) ... End RaiseEvent End Event End Class
Custom events are declared with the Custom modified on the event declaration and have to explicitly state their delegate type. Custom events have three parts: an AddHandler method that is called when someone is hooking up to the event, a RemoveHandler method that is called when someone unhooks from the event and a RaiseEvent method that is called when the class does a RaiseEvent on the event. The AddHandler and RemoveHandler methods take a delegate of the type of the event. The RaiseEvent method takes the same parameters as the event delegate does. So, to store all event delegates in one hashtable, you could do the following:
Class C1 Private EventDelegates As New Dictionary(Of String, EventHandler)
Private Sub AddNewHandler(ByVal eventName As String, ByVal handler As EventHandler) If EventDelegates.ContainsKey(eventName) Then EventDelegates(eventName) = CType([Delegate].Combine(EventDelegates(eventName), handler), EventHandler) Else EventDelegates(eventName) = handler End If End Sub
Private Sub RemoveExistingHandler(ByVal eventName As String, ByVal handler As EventHandler) If EventDelegates.ContainsKey(eventName) Then EventDelegates(eventName) = CType([Delegate].Remove(EventDelegates(eventName), handler), EventHandler) End If End Sub
Private Sub RaiseOneEvent(ByVal eventName As String, ByVal sender As Object, ByVal e As EventArgs) If EventDelegates.ContainsKey(eventName) Then Dim p As EventHandler = EventDelegates(eventName)
If p IsNot Nothing Then p.Invoke(sender, e) End If End If End Sub
Public Custom Event MyEvent As EventHandler AddHandler(ByVal d As EventHandler) AddNewHandler("MyEvent", d) End AddHandler
RemoveHandler(ByVal d As EventHandler) RemoveExistingHandler("MyEvent", d) End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As EventArgs) RaiseOneEvent("MyEvent", sender, e) End RaiseEvent End Event End Class
One thing to notice that's different from C# is that we make you specify a RaiseEvent method. This is to enable the RaiseEvent statement, which C# doesn't have, to work properly. Otherwise, it works pretty much the same way C#'s event declaration does.