一个关于委托的例子(排序)
来源:互联网 发布:贵州省大数据局招聘 编辑:程序博客网 时间:2024/05/17 05:16
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
程序运行结果
- 一个关于委托的例子(排序)
- 一个关于委托事件的例子(计时器)
- 一个委托的例子
- C#中关于委托练习的一个例子
- 一个委托的简单例子
- C#一个委托的例子
- C# 关于委托的例子
- 写的委托的一个简单例子
- 一个.NET委托的入门例子
- C#委托回调的一个例子
- C#关于委托与事件的例子
- 关于一个可见性和指令重排序的例子
- 委托delegate的学习.+一个简单的委托和委托链的例子
- 委托delegate的学习.+一个简单的委托和委托链的例子
- 冒泡排序的一个例子
- 定制排序的一个例子
- C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介
- C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介
- 语法分析器的编写!急!
- 在运行时改变JFormattedTextField控件的输入模式
- 如何把资源中的文件写入磁盘
- 框架和MVC
- 2005年山东省计算机技术技能大赛心得!
- 一个关于委托的例子(排序)
- Ant简介
- 关于微内核的一点思考
- 奇怪的索引
- OOP技术基本概念
- 最简单的Struts程序(4)---Struts应用的国际化(参考孙卫琴的struts一书)
- 大道至简 Java 23种模式一点就通
- 05/11/29
- asp.Net导出Excel的快捷方法