VB.Net program to demonstrate a simple delegate

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

Here, we will create a class with a method and also declare a delegate according to the signature of the method. The delegate is similar to the function pointer in c. It holds the address of the function. We can call the function using delegates.

Program/Source Code:

The source code to demonstrate a simple delegate is given below. The given program is compiled and executed successfully.

' program to demonstrate a simple delegate.

Public Delegate Sub MyDelegate()
Class Sample
    Public Sub SayHello()
        Console.WriteLine("Hello World")
    End Sub
End Class

Module Module1
    Sub Main()
        Dim S As New Sample()
        Dim del As MyDelegate = AddressOf S.SayHello

    End Sub
End Module


Hello World
Press any key to continue . . .


In the above program, we created a class Sample that contains a method SayHello, and we declared a delegate according to the signature of the method defined in the class.

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 Sample class and then assigned the address of the method to the delegate and call the method of the class using delegate that will print the "Hello World" message on the console screen.

VB.Net Basic Programs »



Comments and Discussions



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

© some rights reserved.