可以用委托
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