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
            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
      RemoveHandler Var1.MyEvent, D1
   End Sub

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


  • 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:

  • 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 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.

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

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date