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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Today's "average" business in general is ever more reliant on technology and the Internet. Mobility is the most often cited business trend that has transformed the way many of us work and communicate. From an IT security perspective, this means that protection methods and tools from even a few years ago are rapidly becoming "unfit for purpose." This guide provides crucial facts to assist you in building a robust business case, meeting the demands of your business, and protecting against threats now and in the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds