ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

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 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 »



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.