VB.Net program to demonstrate a simple delegate with non-member function

Here, we are going to demonstrate a simple delegate with non-member function in VB.Net.
Submitted by Nidhi, on January 03, 2021 [Last updated : March 06, 2023]

Implement a simple delegate with non-member function in VB.Net

Here, we will create a non-member function and also declare a delegate according to the signature of the function. 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 with a non-member function is given below. The given program is compiled and executed successfully.

VB.Net code to implement a simple delegate with non-member function

'VB.net program to demonstrate a simple delegate 
'with non-member function.

Module Module1
    Public Delegate Sub MyDelegate()
    Sub SayHello()
        Console.WriteLine("Hello World")
    End Sub

    Sub Main()
        Dim del As MyDelegate = AddressOf SayHello
        del()
    End Sub
End Module

Output:

Hello World
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains two functions SayHello() and Main(), and we declared a delegate according to the signature of the SayHello() function.

The SayHello() function prints the "Hello World" message on the console screen and the Main() function is the entry point for the program. And, we assigned the address of the SayHello() function to the delegate and called the function using delegate that will print "Hello World" message on the console screen.

VB.Net Basic Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.