Custom Events in VB .NET 2005

This article provides an introduction to Custom Events, a new language feature in VB .NET 2005. Custom Events allow the user to specify what to do when AddHandler, RemoveHandler, and RaiseEvent are called for an event.

Visual Basic 2005 has a number of new language features not present in previous versions of VB.NET. One of these is the ability to create custom events such that the programmer can specify what is done when AddHandler, RemoveHandler and RaiseEvent are called.

Below is an example of VB Code which uses a custom event to log changes to event handling. Of course, this is just one example of how a custom event could be used. The methods could be used for basically any purpose.

Delegate Sub MyDelegate(ByVal message As String)

Class MyClass1
   Custom Event MyEvent As MyDelegate

      ' This code will be run when AddHandler MyEvent, D1
      ' is called
      AddHandler(ByVal value As MyDelegate)
         Console.WriteLine("Adding Handler for MyEvent")
         MyEventHandler = value
      End AddHandler

      ' This code will be run when RemoveHandler MyEvent, D1
      ' is called
      RemoveHandler(ByVal value As MyDelegate)
         Console.WriteLine("Removing Handler for MyEvent")
         MyEventHandler = Nothing
      End RemoveHandler

      ' This code will be run when RaiseEvent MyEvent(string)
      ' is called
      RaiseEvent(ByVal message As String)
         If Not MyEventHandler Is Nothing Then
            MyEventHandler.Invoke(message)
         Else
            Console.WriteLine("No Handler for Raised MyEvent")
         End If
      End RaiseEvent
   End Event

   Public MyEventHandler As MyDelegate

   Public Sub Raise_Event()
      RaiseEvent MyEvent("MyEvent Was Raised")
   End Sub
End Class

Module DelegateModule
   Dim Var1 As MyClass1
   Dim D1 As MyDelegate

   Sub Main()
      Var1 = New MyClass1
      D1 = New MyDelegate(AddressOf MyHandler)
      AddHandler Var1.MyEvent, D1
      Var1.Raise_Event()
      RemoveHandler Var1.MyEvent, D1
   End Sub

   Sub MyHandler(ByVal message As String)
      Console.WriteLine("Event Handled: " & message)
   End Sub
End Module


Comments

  • Issue with more than one subscribers

    Posted by jaraics on 05/06/2009 02:59pm

    This code will not work with more subscribers to the event. It should use the Combine and Remove methods of the Delegate, to add, respectively remove subscribers. AddHandler(ByVal value As MyDelegate) Console.WriteLine("Adding Handler for MyEvent") MyEventHandler = [Delegate].Combine (MyEventHandler, value) End AddHandler RemoveHandler(ByVal value As MyDelegate) Console.WriteLine("Removing Handler for MyEvent") MyEventHandler = [Delegate].Remove(MyEventHandler, value) End RemoveHandler More on events here: http://msdn.microsoft.com/en-us/magazine/cc163533.aspx

    Reply
  • Couple of code quality points

    Posted by David Wheeler on 04/13/2005 07:34am

    There are a few minor deficiencies in the code as provided. Firstly, to follow the event pattern in .NET your delegate should really take two parameters: the sender (as an object reference) and a form of EventArgs object as a second parameter to carry the additional information. Secondly, your delegate should be kept as a Private field to prevent a client from directly accessing it; this is actually the point of the event mechanism in .NET. Thirdly, your AddHandler and RemoveHandler implementations overlook the fact that events are multicasting in .NET. Therefore, you should be combining the delegates in AddHandler and ensuring that only the appropriate delegate is removed in RemoveHandler. If it will help, you can find a more complete example of how to use custom events in the real world at http://davewheeler.blogspot.com/2005/04/new- and-improved-events.html. There's a lot of explanation for both C# and Visual Basic developers, along with a fairly comprehensive set of examples.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds