VB.Net program to demonstrate a simple delegate with a method of a structure

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

Implement a simple delegate with a method of a structure in VB.Net

Here, we will create a structure 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 with a method of a structure is given below. The given program is compiled and executed successfully.

VB.Net code to implement a simple delegate with a method of a structure

'VB.net program to demonstrate a simple delegate 
'with a method of a structure.

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

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 structure Sample that contains a method SayHello, and we declared a delegate according to the signature of the method defined in the structure.

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 structure 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.