A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, delegates are object-oriented, type safe, and secure. The type of a delegate is defined by the name of the delegate. The following example declares a delegate named mydlg that can encapsulate a method :
How we use delegate in VB.net programming:
public delegate sub mydlg()
Then we use the delegate by simply declaring a variable of the delegate and assigning the sub or function to run when called. First the sub to be called:
Private Sub message()
Console.WriteLine ( "show message" )
End Sub
now it matches our declaration of MyDlg. it's a sub routine with no parameters. And then our test code:
Dim dlg As mydel
dlg = New mydel(AddressOf message)
dlg.Invoke()
When we invoke the delegate, the message sub is run.
The following code define the delegate:


Join the conversation! Your thoughts help the community grow.