Delegates are objects that can call the methods of other objects for you. For this reason, delegates
are sometimes described as type-safe function pointers. Visual Basic.NET has moved to an event model that
is based on delegates. Although you can create your own delegates, in most cases Visual Basic.Net will
create the delegate and take care of the details for you. But in certain case you have to create them. Here is
a sample that demonstrate the creation and use of Delgates with Visual Basic.NET. To get it to run in Visual
Studio.NET, create a new console application and paste the code below into Module1.vb.




 
'Applicatio : Console
'History :
Module Module1
'Define a Delegate
Delegate Sub CompareFunc(ByVal x As Integer, _
ByVal y As Integer, ByRef b As Boolean)
'Define a Event that will be used in AddHandler
Public Event a As CompareFunc
'Bubble Sort Algorithm which calles Delegates to perform work
Function BubbleSort(ByVal SortHigher As CompareFunc, _
ByVal IntArray() As Integer) As Integer()
Dim I, J, Value, Temp As Integer
Dim b As Boolean
For I = 0 To Ubound(IntArray)
Value = IntArray(I)
For J = I + 1 To UBound(IntArray)
Try
SortHigher.Invoke(IntArray(J), Value, b)
If b = True Then
Temp = IntArray(J)
IntArray(J) = Value
IntArray(I) = Temp
Value = Temp
End If
Catch
'Add error handling here, or just proceed
End Try
Next J
Next I
End Function
'Actual Event handling , called by Delgates
Sub Fire(ByVal x As Integer, ByVal y As Integer, ByRef b As Boolean)
If y > x Then
b = True
Else
b = False
End If
End Sub
'Entry point
Sub Main()
Dim IntArray() As Integer = {12, 1, 96, 56, 70}
Dim iArrayCount As Integer
Dim iCounter As Integer
' Add a delegate Handler
AddHandler a, (AddressOf Fire)
'Define Array for Sorting
iArrayCount = IntArray.Length
Console.WriteLine("Integer array to be sorted")
For iCounter = 0 To iArrayCount - 1
Console.WriteLine(" Value at{0} is {1} ", _
iCounter, IntArray(iCounter))
Next
'Call the method which is going to raise events
BubbleSort(Module1.aEvent, IntArray)
iArrayCount = IntArray.Length
Console.WriteLine("Integer array after Bubble Sort with Delegate")
'Display the Data to user on Console
For iCounter = 0 To iArrayCount - 1
Console.WriteLine(" Value at{0} is {1}", _
iCounter, IntArray(iCounter))
Next
End Sub
End Module