Lore,
| But if the Invoke method is like calling the method directly. Why does
this
| example use delegates? Do you have got an idea?
It allows how the two items are compared to be changed.
Consider the following:
Function CompareLessThan(ByVal X As Integer, _
ByVal Y As Integer) As Boolean
If X < Y Then
Return True
Else
Return False
End If
End Function
| Dim Sort As New SortClass()
| Dim arr1() As Integer = {1, 5, 3, 2, 7, 22, 5, 54, 12}
Sort.SelectionSort(AddressOf Sort.CompareLessThan, arr1)
| MsgBox("Array sorted.")
Notice the SelectionSort routine didn't changed, however now the values are
descending instead of ascending.
Granted in the case of Integers its not as important as the case of Domain
Objects
| Delegate Function Compare(ByVal x As Person, _
| ByVal y As Person) As Boolean
| Sub SelectionSort(ByVal comparison As Compare, _
| ByVal people() As Person)
Function CompareName(ByVal X As Person, _
ByVal Y As Person) As Boolean
If x.Name > y.Name Then
Return True
Else
Return False
End If
End Function
Function CompareAge(ByVal X As Person, _
ByVal Y As Person) As Boolean
If x.Age > y.Age Then
Return True
Else
Return False
End If
End Function
Or even a complex comparison that compares multiple properties.
Function CompareAddress(ByVal X As Person, _
ByVal Y As Person) As Boolean
If x.State > y.State Then
Return True
ElseIf x.State < y.State Then
Return False
ElseIf x.City > y.City Then
Return True
ElseIf x.City < y.City Then
Return False
ElseIf x.Street > y.Street Then
Return True
ElseIf x.Street < y.Street Then
Return False
Else
Return False
End If
End Function
Dim people() As Person
Sort.SelectionSort(AddressOf Sort.CompareName, people)
Sort.SelectionSort(AddressOf Sort.CompareAge, people)
Sort.SelectionSort(AddressOf Sort.CompareAddress, people)
Note the "standard" convention is for Compare to return an Integer that is
either less, greater or equal to 0. To indicate if the parameters are less,
greater or equal to each other.
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
"Lore Leunoeg" <lo*********@gmx.net> wrote in message
news:dk**********@news01.versatel.de...
| Hello Jay
| Thank you.
| But if the Invoke method is like calling the method directly. Why does
this
| example use delegates? Do you have got an idea?
|
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/vblr7/html/vastmDelegate.htm
| Sincerely
| Lore
| --- example: ---
| Public Class SortClass
| Delegate Function Compare(ByVal x As Integer, _
| ByVal y As Integer) As Boolean
|
| Function CompareValues(ByVal X As Integer, _
| ByVal Y As Integer) As Boolean
| If X > Y Then
| CompareValues = True
| Else
| CompareValues = False
| End If
| End Function
|
| Sub SelectionSort(ByVal IsGreaterThan As Compare, _
| ByVal IntArray() As Integer)
| Dim MaxVal As Integer
| Dim MaxIndex As Integer
| Dim i, j As Integer
|
| ' Step through the elements in the array starting with the
| ' last element in the array.
| For i = UBound(IntArray) To 1 Step -1
| MaxVal = IntArray(i)
| MaxIndex = i
| For j = 1 To i
| ' Use the delegate to compare values.
| If IsGreaterThan.Invoke(IntArray(j), MaxVal) Then
| MaxVal = IntArray(j)
| MaxIndex = j
| End If
| Next j
| ' Use the delegate to compare values.
| If IsGreaterThan.Invoke(i, MaxIndex) Then
| IntArray(MaxIndex) = IntArray(i)
| IntArray(i) = MaxVal
| End If
| Next i
| End Sub
| End Class
|
| Class Class1
| Sub SortArray()
| Dim Sort As New SortClass()
| Dim arr1() As Integer = {1, 5, 3, 2, 7, 22, 5, 54, 12}
| Sort.SelectionSort(AddressOf Sort.CompareValues, arr1)
| MsgBox("Array sorted.")
| End Sub
| End Class
|
| ' Add a button to your main form and insert the following code
| ' into the Click event handlr.
| Private Sub Button1_Click(ByVal sender As System.Object, _
| ByVal e As System.EventArgs) _
| Handles Button1.Click
| Dim c As New Class1()
| c.SortArray()
| End Sub
| --- end example ---
|
<<snip>>