ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

VB.Net program to demonstrate the multicast delegate

Here, we are going to demonstrate the multicast delegate in VB.Net.
Submitted by Nidhi, on January 03, 2021

Here, we will demonstrate multicast delegate by creating a class with two methods and also declare a delegate according to the signature of methods, both methods must have the same number of parameters and return types.

Program/Source Code:

The source code to demonstrate the multicast delegate is given below. The given program is compiled and executed successfully.

'VB.net program to demonstrate the multicast delegate.

Public Delegate Sub MyDelegate(ByVal num1 As Integer, ByVal num2 As Integer)

Class CalC
    Public Sub Addition(ByVal num1 As Integer, ByVal num2 As Integer)
        Dim result As Integer
        result = num1 + num2
        Console.WriteLine("Addition is: {0}", result)

    End Sub

    Public Sub Subtraction(ByVal num1 As Integer, ByVal num2 As Integer)
        Dim result As Integer
        result = num1 - num2
        Console.WriteLine("Subtraction is: {0}", result)
    End Sub
End Class 

Module Module1
    Sub Main()
        Dim cal As New CalC()

        Dim del1 As MyDelegate = AddressOf cal.Addition
        Dim del2 As MyDelegate = AddressOf cal.Subtraction

        Dim mulDel As MyDelegate = MulticastDelegate.Combine(del1, del2)
        mulDel(20, 10)
    End Sub
End Module

Output:

Addition is: 30
Subtraction is: 10
Press any key to continue . . .

Explanation:

In the above program, we created a class CalC class that contains two methods Addition() and Subtraction(), and we also declared a delegate according to the signature of methods.

After that, we created a module Module1 that contains the Main() method, the Main() method is the entry point for the program. And, we created an object of the CalC class and then assigned the address of methods to the delegates one by one. Then we combined the delegated using MulticastDelegate.Combine() method. After that, we call both methods using a new delegate that will print Addition and subtraction of numbers on the console screen.

VB.Net Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.