×

VB.Net Programs

VB.Net program to demonstrate a simple delegate

By Nidhi Last Updated : November 11, 2024

Implement a simple delegate in VB.Net

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.

VB.Net code to implement a simple delegate

'VB.net 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

        del()
    End Sub
End Module

Output:

Hello World
Press any key to continue . . .

Explanation:

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!

Load comments ↻





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