What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleAdd delegates to combine lists of methods in Visual Basic .NET
DescriptionThis example shows how to add delegates to combine lists of methods in Visual Basic .NET.
Keywordsdelegate, example, example program, Windows Forms programming, Visual Basic .NET, VB.NET
CategoriesSoftware Engineering, Software Engineering, Miscellany

Delegates are a confusing topic. A delegate is a data type much like Integer or Boolean but a delegate represents a method (subroutine or function) instead of a simple piece of data. Specific delegate types represent methods that have specific signatures. For example, a delegate might represent a function that takes two Integer parameters and returns a Single result.

After a program defines a delegate type, it can declare variables of that type. It can then store a reference to a method in a delegate variable and later invoke the method by calling whatever method is in the variable. For an example of using delegates, see the example Use delegates to pass a method's address to another method in Visual Basic .NET.

As if that wasn't confusing enough, you can add delegates. So what does it mean to add delegates? If you add two delegates, the result is a new delegate that executes the first followed by the second. You can sort of think of a delegate as a list of references to methods. If you add two delegates, the second one's methods get added to the first one's list. Later if you execute the combined delegate, both lists are executed.

This example uses the following code to demonstrate delegate addition.

' A delegate that represents a method that adds something to
' the ListBox.
Private Delegate Sub AddToListDelegate()

' Specific methods that match the delegate.
Private Sub MethodA()
    lstResults.Items.Add("    This is MethodA")
End Sub

Private Sub MethodB()
    lstResults.Items.Add("    This is MethodB")
End Sub

' Define and use three delegates.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Define delegate variables.
    Dim A As AddToListDelegate = AddressOf MethodA
    Dim B As AddToListDelegate = AddressOf MethodB
    Dim C As AddToListDelegate = _
        DirectCast([Delegate].Combine(A, B), _

    ' Use the delegates.
    lstResults.Items.Add("Calling A:")
    lstResults.Items.Add("Calling B:")
    lstResults.Items.Add("Calling C:")
    lstResults.Items.Add("Calling C - A:")
    C = DirectCast([Delegate].Remove(C, A), _
End Sub
The code first declares the type AddTolistDelegate. A delegate of this type can hold a reference to a subroutine that takes no parameters.

Next the code defines two methods that match the delegate type.

The form's Load event handler defines two delegate variables A and B, and initializes them so they refer to MethodA and MethodB. It then creates a third delegate C and sets it equal to A + B. It uses the Delegate class's Combine method to add the two delegates and uses DirectCast to convert the result into an AddTolistDelegate object.

The code then executes the delegates. It calls the method referred to by A, then B, and then C. The delegate C contains references to A and B so invoking it makes MethodA and MethodB both execute.

Next the code removed A from C. That removes the call to A's method from the "list" contained in C leaving only B. When the code invokes the result, only MethodB executes.

Note that you can subtract a delegate more than once from another delegate without harm. In this example, the code could remove A from C many times without causing any problems.

However, if you subtract the last delegate from another delegate, then the result is Nothing and you can no longer invoke that result. In this example if the code removed B from C, then C would become Nothing and trying to invoke C would throw an exception.

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.