More on Delegates and Events in VB 2005

August 31, 2007 at 6:21 pm Leave a comment

In the .NET 2.0 Framework, with Visual Basic 2005, there are a number of ways to define and use delegates, and even more ways to define and use events . This post summarizes these differences.

Using  delegates

To use delegates you need three things:

  1. A delegate declaration: A declaration of the signature of the handler procedure.
    (A class is actually defined behind the scenes in VB.)
  2. A delegate object: An instance of the delegate type defined above which holds a pointer to the actual handler procedure.
  3. A handler: The function that is called indirectly by the delegate.

When a call is made to the delegate, the call is forewarned to the handler.

There are three places that you need to add code to your project:

  1. Outside the other class declarations:
    • Declare the delegate using the Delegate keyword. This is really a class declaration of your delegate type. This declaration defines the signature for event handing procedures of this type:
      Delegate Sub BirdsBitsEventHandler(ByVal sMessage As String)
  2. In the class that will send a callback:
    • Declare (Dim) a variable of your delegate type:
      Dim BBMsgDelegate As BirdsBitsMessageHandler
    • Create a New instance of your delegate type:
      BBMsgDelegate = New BirdsBitsMessageHandler _
      (AddressOf Form2.IncomingBirdsBitsMessage)
    • Indirectly call the event handler by calling Invoke on the delegate:
      BBMsgDelegate.Invoke("This is a test!")
  3. In the module that will receive the callback:
    • Define the actual handler procedure:
      Public Sub IncomingBirdsBitsMessage(ByVal sMessage As String)
      TextBox1.Text = sMessage
      End Sub

Exposing  and Handling Events

To use events, there are still three things you need:

  1. A declaration of a delegate.
    (VB can generate a hidden delegate declaration when the event is declared.)
  2. A declaration of the event.
  3. An event handler.

A class that exposes an event defines a private (hidden) delegate field that holds pointers to all the clients that subscribe to the event. There are a number of ways to add events and event handlers to a classes.

There are two or three places (depending on your approach) that you need to add code to your project:

  1.  Outside the other class declarations:
    (This step is only required for the first option in step 2.)

    • Declare the delegate using the Delegate keyword.
      Delegate Sub AFoundEventHandler(ByVal sMessage As String)
  2. In the class that will send events:
    • Declare and raise an event of the delegate type.
      • Use the Event keyword to declare an event:
        Public Event AFound As AFoundEventHandler
      • Use the RaiseEvent keyword to send an event.
        Dim e As New EventArgs
        RaiseEvent AFound(Me, e)
    • Or, declare and use an event type defined by the .NET Framework.
      This is the preferred approach!  (You can skip step 1, since the delegate for this event is defined in the .NET Framework.)

      • Use the Event keyword to declare an event of the type System.EventHandler. Public Event AFound As EventHandler
      • Use the RaiseEvent keyword to send an event.
        Dim e As New EventArgs
        RaiseEvent AFound(Me, e)
    • Or, Declare an event and a delegate in one statement: (If you use this option, you don’t need step 1 above since the delegate is declared along with the event)
      • Use the Event keyword to declare an event. VB generates a hidden delegate with the signature of the event:
        Public Event AFound(ByVal sender As Object, ByVal e As EventArgs)
      • Use the RaiseEvent keyword to send an event.
        Dim e As New EventArgs
        RaiseEvent AFound(Me, e)
  3.  In the module that instantiates the event class:
    • Create an object for use with events.
      • Use theWithEvents keyword to create an instance of the class:
        Dim WithEvents LetGet as new LetterGetter()
      • Use the Handles keyword to define an event handler:
        Private Sub LetterGetter_AFound(ByVal sender As Object, ByVal e As EventArgs) Handles LetGet.AFound
        ' Do something
        End Sub
    • Or, add an event handler to an existing object:
      • Declare an instance of the event class:
        Dim LetGet as new LetterGetter()
      • Define a subroutine to handle the event:
        Private Sub AFoundHandler(ByVal sender As Object, ByVal e As EventArgs)
        ' Do something
        End Sub
      • Add a subroutine to handle the event using the Add Handler keyword:
        AddHandler LetGet.AFound, AddressOf AFoundHandler
      • Remove the handler (when no longer needed) using the Remove Handler keyword :
        RemoveHandler Obj.Ev_Event, AddressOf EventHandler
Advertisements

Entry filed under: .NET, Events, Visual Basic. Tags: , .

.NET Remoting: The Simplest Remoting Example .NET Remoting with Events in VB 2005 and .NET 2.0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Bird’s Bits

Computers, software & the Internet

Recent Posts

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 42 other followers


%d bloggers like this: