H o m e

 

C o m b i n i n g    V B    N E T    D e l e g a t e s    

By Michael McIntyre

mikemc@getdotnetcode.com

 

This article provides sample code and an explanation of how to combine delegates to make it possible to call more than one method at the same time.

 

One of the types we as VB.NET programmers can define ourselves is a MulticastDelegate class. We define a MulticastDelegate class so our code can instantiate a MulticastDelegate object that points to static method(s) or to a class instance and instance method(s) of that class.

 

VB.NET implements the Delegate keyword for defining Multicast delegates. The Delegate keyword provides the way you derive from the MulticastDelegate class. This is different than the standard way we define a class with a Class…End Class statement. Here is an example that defines a MulticastDelegate class:

 

Delegate Sub MessageDelegate(ByVal message As String)

 

The line of code is the full definition of a MulticastDelegate class.  The keyword Delegate informs the compiler you are defining a MulticastDelegate class.  The words following the Delegate keyword define a MulticastDelegate class named MessageDelegate.

 

The declaration of a delegate type establishes a contract that specifies the signature of one or more methods.

 

To instantiate a MessageDelegate we use the New keyword.  The example below instantiates a MessageDelegate object which points to a method named DisplayMessage.

 

Private _Display As New MessageDelegate(AddressOf DisplayMessage)

 

A MulticastDelegate can link a list of delegates, called an invocation list, consisting of one or more elements. When a multicast delegate is invoked, the delegates in the invocation list are called synchronously in the order in which they appear. If an error occurs during execution of the list then an exception is thrown.

 

So how do we link MulticastDelegate objects to create an invocation list?  We use the MulticastDelegate’s  Combine method:

 

Example Code – Dispatcher class to call three methods at the same time.

 

Public Class Dispatcher

 

    '  Define a delegate type named MessageDelegate with a

    '  signature of: (ByVal message As String)

    Delegate Sub MessageDelegate(ByVal message As String)

 

    ' Declare a variable of type MessageDelegate named _Display

    ' which points to the DisplayMessage method in this class.

    ' Instantiate a MessageDelegate object and assign it to the variable.

    Private _Display As New MessageDelegate(AddressOf DisplayMessage)

 

    ' Declare a variable of type MessageDelegate named _Write

    ' which points to the WriteMessage method in this class.

    ' Instantiate a MessageDelegate object and assign it to the variable.

    Private _Write As New MessageDelegate(AddressOf WriteMessage)

 

    ' Declare a variable of type MessageDelegate named _Log

    ' which points to the LogMessage method in this class.

    ' Instantiate a MessageDelegate object and assign it to the variable.

    Private _Log As New MessageDelegate(AddressOf LogMessage)

 

    ' Declare a variable of type MessageDelegate named BroadCast.

    Public Broadcast As MessageDelegate

 

    Public Sub New()

        ' Combine the _Display, _Write, and _Log delegates into

        ' the Broadcast MessageDelegate.

        Broadcast = CType(Broadcast.Combine(Broadcast, _Display), _

        MessageDelegate)

 

        Broadcast = CType(Broadcast.Combine(Broadcast, _Write), _

        MessageDelegate)

 

        Broadcast = CType(Broadcast.Combine(Broadcast, _Log), _

        MessageDelegate)

    End Sub

 

    Public Sub DisplayMessage(ByVal message As String)

        MessageBox.Show("Displaying: " & message)

    End Sub

 

    Public Sub WriteMessage(ByVal message As String)

        MessageBox.Show("Writing: " & message)

        Console.WriteLine(message)

    End Sub

 

    Public Sub LogMessage(ByVal message As String)

        MessageBox.Show("Loging: " & message)

        ' Code to write to log file goes

    End Sub

 

End Class

 

Use the Dispatcher Class

 

Now we can use the Dispatcher class in another class, for example in a System.Windows.Forms Form class.  Here is a demonstration.

 

' Declare a variable named theDispatcher of type Dispatcher.

' Instantiate a Dispatcher object and assign it to the variable.

Dim theDispatcher As New Dispatcher()

' Call theDispatcher's BroadCast delegate passing it a string to dispatch.

theDispatcher.Broadcast("Test Message")

 

When theDispatcher’s Broadcast delegate is called, it calls the _Display, _Write, and _Log methods in theDispatcher object, passing “Test Message” into each method as it calls it.

 

Learn more at the links below:

 

Delegate Class

EventHandlerList Class

 

Copyright © 2001-2005 aZ Software Developers. All rights reserved.