More on Delegates and Events in VB 2005
August 31, 2007
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:
- A delegate declaration: A declaration of the signature of the handler procedure.
(A class is actually defined behind the scenes in VB.) - A delegate object: An instance of the delegate type defined above which holds a pointer to the actual handler procedure.
- 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:
- 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)
- 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:
- 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!")
- Declare (Dim) a variable of your delegate type:
- 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
- Define the actual handler procedure:
Exposing and Handling Events
To use events, there are still three things you need:
- A declaration of a delegate.
(VB can generate a hidden delegate declaration when the event is declared.) - A declaration of the event.
- 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:
- 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)
- Declare the delegate using the Delegate keyword.
- 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)
- Use the Event keyword to declare an event:
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)
- Use the Event keyword to declare an event of the type System.EventHandler.
- 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)
- Use the Event keyword to declare an event. VB generates a hidden delegate with the signature of the event:
- Declare and raise an event of the delegate type.
- 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
- Use theWithEvents keyword to create an instance of the class:
- 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
- Declare an instance of the event class:
- Create an object for use with events.
Entry Filed under: .NET, Events, Visual Basic. .
Trackback this post | Subscribe to the comments via RSS Feed