Module Module1 Public Enum comparison theFirstComesFirst = 1 theSecondComesFirst = 2 End Enum 'A simple class to hold two items Public Class Pair 'Private array to hold the two objects Private thePair(2) As Object 'The delegate declaration Public Delegate Function WhichIsFirst(ByVal obj1 As Object, ByVal obj2 As Object) As comparison 'Passed in constructor takes two objects, 'added in order received Public Sub New(ByVal firstObject As Object, ByVal secondObject As Object) thePair(0) = firstObject thePair(1) = secondObject End Sub 'Public method that orders the 'two objects by whatever criteria the objects like! Public Sub Sort(ByVal theDelegatedFunc As WhichIsFirst) If (theDelegatedFunc(thePair(0), thePair(1)) = comparison.theSecondComesFirst) Then Dim temp As Object = thePair(0) thePair(0) = thePair(1) thePair(1) = temp End If End Sub 'Public method that orders the 'two objects by the reverse of whatever criteria the 'objects likes! Public Sub ReverseSort(ByVal theDelegatedFunc As WhichIsFirst) If (theDelegatedFunc(thePair(0), thePair(1)) = comparison.theFirstComesFirst) Then Dim temp As Object = thePair(0) thePair(0) = thePair(1) thePair(1) = temp End If End Sub 'Ask the two objects to give their string values Public Overrides Function ToString() As String Return thePair(0).ToString() + ", " + thePair(1).ToString() End Function End Class Public Class Dog Private weight As Integer Public Sub New(ByVal weight As Integer) Me.weight = weight End Sub 'Dogs are sorted by weight Public Shared Function WhichDogComesFirst(ByVal o1 As Object, ByVal o2 As Object) As comparison Dim d1 As Dog = DirectCast(o1, Dog) Dim d2 As Dog = DirectCast(o2, Dog) If (d1.weight > d2.weight) Then Return comparison.theSecondComesFirst Else Return comparison.theFirstComesFirst End If End Function Public Overrides Function ToString() As String Return weight.ToString() End Function End Class Class Tester Public Sub Run() 'Create two students and two dogs 'and add them to a Pair object Dim Milo As Dog = New Dog(65) Dim Fred As Dog = New Dog(12) Dim dogPair As Pair = New Pair(Milo, Fred) Console.WriteLine("dogPair" + vbTab + vbTab + vbTab + vbTab + ": {0}", dogPair.ToString()) 'Instantiate the delega Dim theDogDelegate As Pair.WhichIsFirst = New Pair.WhichIsFirst(AddressOf Dog.WhichDogComesFirst) 'Sort using the delegate dogPair.Sort(theDogDelegate) Console.WriteLine("After Sort dogPair" + vbTab + vbTab + ": {0}", dogPair.ToString()) dogPair.ReverseSort(theDogDelegate) Console.WriteLine("After ReverseSort dogPair" + vbTab + ": {0}", dogPair.ToString()) End Sub End Class Sub Main() Dim t As Tester = New Tester() t.Run() Console.ReadLine() End Sub End Module