Delegates:
A delegate is a special class that represent a method signature. It is strongly typed. The method signature that we design must be used exactly in all methods that will implement the delegates. They are used to implement callbacks and events in VB.NET – “notify me when it is my turn to”
Delegates are similar to function pointers in C, with some exceptions.
To use a delegate:
1: Define a delegate
2: Create an instance of the delegate
3: Use the AddressOf to get a reference to the real procedure, and store it in delegates instance.
4: When the instance is invoked, its actually the procedure that is run, using any parameters that are passed into the delegates instance.
5: The delegates instance can be re-assigned to refer to a different procedure if desired.
Each instance could be spun off on its own thread (Asynchronous multicast delegates) to perform in an asynchronous method (prevents blocking on long-running callbacks).
A delegate is special class, that represent a method signature.
Strongly typed – that means the method signature you define must be used exactly in all methods that will implement the delegate.
Delegates are used to implement callback and events in VB.NET (notify me when it’s my turn to)
Delegates may return any allowable type.
Each delegate instance could be spun off on its own thread (Asynchronous Multicasting Delegates) to perform in an asynchronous manner. This prevents blocking on long-running callbacks.
Basic steps:
Define a delegate.
Create an instance of the delegate.
Use the AddressOf to get a reference to the real procedure and store it in delegate instance.
When the instance is invoked, its actually the procedure that is run, using the parameters that are passed into the delegate instance.
The delegate instance can be re-assigned to refer to a different procedure if desired.
Multicast delegates:
Another interesting thing with delegates is that they can refer to multiple target sub/functions. This means that a delegate can hold a list of functions that are invoked when you invoke the delegate
Example
' define the delegate
' when you define a delegate you are really creating a new type that represents the
' "signature" of a funtion. You are defining what paramters are used and what the return type is.
Delegate Sub PrintMessage(ByVal msg As String)
' Here we define a method that has the same "signature" as the delegate. That means that it as ‘ the same paramters and return types as the delegate definition above
Sub PrintHello(ByVal name As String)
Console.WriteLine("Hello, " + name)
End Sub
Sub PrintGoodbye(ByVal name As String)
Console.WriteLine("Goodbye, " + name)
End Sub
Sub Main()
' Create an instance of the delegate that
' will be used to refer to a mehtod at runtime
Dim pm As PrintMessage
' use the AddressOf keyword to obtain a reference to
' a real function/sub and store it in the delegate.
pm = AddressOf PrintHello
' Now we can use the delgate just as if it were
' a real funtion/sub
pm("Dave")
' The cool thing with delegates is that you
' can change around what method is beign invoked
' by the delegate at runtime
pm = AddressOf PrintGoodbye
' Notice that this is the same call as above
' but this time it will say goodbye. This is very
' usefull for things such as callbacks. It is also
' the basis for events. In fact when you use the Event
' keyword in VB it is creating a delegate behind the scenes
' for you
pm("Dave")
‘’ Multicast delegates:
' another interesting thing with delegates is that they
' can refer to multiple target sub/functions. This means that
' a delegate can hold a list of functions that are invoked when
' you invoke the delegate
pm = AddressOf PrintHello
pm = pm.Combine(pm, CType(AddressOf PrintGoodbye, PrintMessage))
' now, when the delegate is invoked it will call both methods!!!
pm("Steve")
End Sub
Add a comment