Multicasting:
Multicasting enables you to combine several
handler methods in a list so that all of them are bound to the same delegate
object. When Invoke is called on the delegate object, the MulticastDelegate
class provides the code to execute each handler method in the list.
Defining Multicasting:
In VB.NET taking the method for two different
delegate object objects and combing them into a multicast delegate object.
Public Delegate Sub
Bank()
'Create two indiviual delegate
Dim obj1, obj2
As Bank
obj1 = AddressOf Account.Deposit
obj2 = AddressOf Account.withdraw
'Combine deligate into multicast delegate
Dim result
As [Delegate] = [Delegate].Combine(obj1, obj2)
'Convert referance to banktype
Dim handlers
As Bank
hannlers=CType(result,Bank)
Note:
-
The use of square bracket around the name of the [Delegate] class . These square bracket are required because deligate is also a key word in VB.NET they serve as escape characters telling the visual basic compiler that you indented to use delegate as class name and not a keyword
-
The call to combine returns a reference to a newly created multicast deligate object. This conversion is required if you wants to call the invoke method.
Coding for Multicasting:
Module
Module1
Public Delegate Sub Bank()
Public Class
Account
Public Sub
Diposit()
Console.WriteLine("you are successfully
Deposit")
End Sub
End Class
Class Chal :
Inherits Account
Public Sub
Withdraw()
Console.WriteLine("you are successfully
withdraw")
End Sub
End Class
Sub Main()
Dim obj As New Chal
Dim obj1 As
Bank
Dim obj2 As
Bank
obj1 = New Bank(AddressOf
obj.Diposit)
obj2 = New Bank(AddressOf
obj.Withdraw)
Dim result As
[Delegate] = [Delegate].Combine(obj1, obj2)
Dim handlers As
Bank
handlers = CType(result, Bank)
handlers.Invoke()
End Sub
End Module
Output:
