views:

79

answers:

1

I'm looking for some pointers on implementing Custom Events in VB.NET (Visual Studio 2008, .NET 3.5).

I know that "regular" (non-custom) Events are actually Delegates, so I was thinking of using Delegates when implementing a Custom Event. On the other hand, Andrew Troelsen's "Pro VB 2008 and the .NET 3.5 Platform" book uses Collection types in all his Custom Events examples, and Microsoft's sample codes match that line of thought.

So my question is: what considerations should I have when choosing one design over the other? What are the pros and cons for each design? Which of these resembles the inner-implementation of "regular" events?

Below is a sample code demonstrating the two designs.

Public Class SomeClass
    Private _SomeEventListeners As EventHandler
    Public Custom Event SomeEvent As EventHandler
        AddHandler(ByVal value As EventHandler)
            _SomeEventListeners = [Delegate].Combine(_SomeEventListeners, value)
        End AddHandler

        RemoveHandler(ByVal value As EventHandler)
            _SomeEventListeners = [Delegate].Remove(_SomeEventListeners, value)
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
            _SomeEventListeners.Invoke(sender, e)
        End RaiseEvent
    End Event

    Private _OtherEventListeners As New List(Of EventHandler)
    Public Custom Event OtherEvent As EventHandler
        AddHandler(ByVal value As EventHandler)
            _OtherEventListeners.Add(value)
        End AddHandler

        RemoveHandler(ByVal value As EventHandler)
            _OtherEventListeners.Remove(value)
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
            For Each handler In _OtherEventListeners
                handler(sender, e)
            Next
        End RaiseEvent
    End Event
End Class
+2  A: 

I would say use the simple delegate; simply - it is already (internally) a list, so you are duplicating effort by wrapping it in List<T>. You also have the overhead of extra list objects and arrays, that could be null, so more impact on garbage collection etc.

Also, if you are doing lots of this, consider EventHandlerList, which exists to provide efficient access to sparse events - i.e. where you want to expose lots of events but many of them can be unassigned.

The first example is far closer to "standard" events (although you might want to watch for unassigned / null handlers when calling Invoke, as it could be null). Additionally, note that some languages (I honestly don't know what VB does here) applies synchronization to events, but in reality very few events really need to be thread-safe, so that could be overkill.

edit it also occurs that there is a functional difference between them:

  • the delegate approach treats different instances with the same target method / instance as equal (I don't think the List<T> will)
  • duplicate delegates: delegate removes last first; list removes earliest first
  • composites: add A, add B, and remove (A+B) - with a delegate this should yield null / empty, but the list approach will retain A and B (with the (A+B) remove failing to find anything)
Marc Gravell
Thank you for your answer, you'vew made things clearer for me. Accepted!
M.A. Hanin