1. Computing

Multicast Delegates

Calling More Than One Delegate Procedure

By

We've seen that delegates really hold the address of procedures. In fact, they can hold more than one address and when a delegate contains more than one address, it's called a multicast delegate. The list of addresses is called an invocation list. In the case of an event that calls a multicast delegate, all of the addresses in the invocation list get called.

To make that work, VB.NET provides the Combine function. Just assign both addresses to the same delegate and combine them:

Dim myDel1 As MyDelegate = _
   New MyDelegate(AddressOf SayHelloInEnglish)
Dim myDel2 As MyDelegate = _
   New MyDelegate(AddressOf SayHelloInDeutch)
Dim myDelBoth As MyDelegate = _
   [Delegate].Combine(myDel1, myDel2)myDelBoth.Invoke()

(Note: The delegate subroutines would also have to be modified with something like ...

lblHello.Text &= vbCrLf & "Guten Tag"

... to avoid replacing the text in the label.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Using VB.NET
  5. Delegates in VB.NET - Using Visual Basic .NET Delegates

©2014 About.com. All rights reserved.