Public Class frmDelegate
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(16, 24)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(328, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "降序排列"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(16, 56)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(328, 23)
Me.Button2.TabIndex = 1
Me.Button2.Text = "升序排列"
'
'TextBox1
'
Me.TextBox1.BackColor = System.Drawing.SystemColors.HighlightText
Me.TextBox1.Location = New System.Drawing.Point(16, 96)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ReadOnly = True
Me.TextBox1.Size = New System.Drawing.Size(328, 21)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = "TextBox1"
'
'frmDelegate
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(360, 130)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
Me.Name = "frmDelegate"
Me.Text = "一个关于委托的排序算法"
Me.ResumeLayout(False)
End Sub
#End Region
Dim c As New Class1
Dim i As Object
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = ""
c.DESCArray()
For Each i In c.arr1
TextBox1.Text &= i.ToString & ", "
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
c.ASCArray()
For Each i In c.arr1
TextBox1.Text &= i.ToString & ", "
Next
End Sub
Private Sub frmDelegate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = ""
For Each i In c.arr1
TextBox1.Text &= i.ToString & ", "
Next
End Sub
End Class
Class Class1
Public arr1() As Object = {1, 86, 5, 3, 2, 7, 23, 12, 0, 55, 5, 25, 99, 51}
'Public arr1() As Object = {"E", "B", "e", "D", "EE", "F", "CC", "DD", "A", "AA", "BCD", "a", "bb"}
Private sort As New SortClass
Sub DESCArray()
sort.sort(AddressOf sort.greaterThan, arr1)
End Sub
Sub ASCArray()
sort.sort(AddressOf sort.lessThan, arr1)
End Sub
Private Class SortClass
Delegate Function Compare(ByVal x, ByVal y) As Boolean
Function greaterThan(ByVal X, ByVal Y) As Boolean
If X > Y Then
Return True
Else
Return False
End If
End Function
Function lessThan(ByVal X, ByVal Y) As Boolean
If X > Y Then
Return False
Else
Return True
End If
End Function
Sub sort(ByVal isCompare As Compare, ByVal array() As Object)
Dim tmp As Object
Dim i, j As Integer
For i = 0 To array.Length - 1
For j = (i + 1) To array.Length - 1
If isCompare.Invoke(array(j), array(i)) Then
tmp = array(i)
array(i) = array(j)
array(j) = tmp
End If
Next j
Next i
End Sub
End Class
End Class
程序运行结果