2010-08-26 66 views
1

我很困惑在引发事件实例和使用委托作为函数指针:与活动困惑

我与低于该分拣整数阵列,而不改变创建一个通用排序功能此功能指针示例程序明确主排序功能。

' Returns True if need to swap 
Delegate Function CompareFunc(ByVal x As Integer, ByVal y As Integer) As Boolean 

' Here are two alternative target methods for the delegate—one for an ascending sort and one for a 

' descending sort: 

Function SortAscending(ByVal x As Integer, ByVal y As Integer) As Boolean 
    If y < x Then 
    SortAscending = True 
    End If 
End Function 

Function SortDescending(ByVal x As Integer, ByVal y As Integer) As Boolean 
    If y > x Then 
    SortDescending = True 
    End If 
End Function 

' Now we can define the sort routine. Note the call to the Invoke method of the delegate: 

Sub BubbleSort(ByVal CompareMethod As CompareFunc, ByVal IntArray() As Integer) 
    Dim i, j, temp As Integer 
    For i = 0 To Ubound(IntArray) 
    For j = i + 1 To Ubound(IntArray) 
     If CompareMethod.Invoke(IntArray(i), IntArray(j)) Then 
     Temp = IntArray(j) 
     IntArray(j) = IntArray(i) 
     IntArray(i) = Temp 
     End If 
    Next j 
    Next i 
End Sub 

' Here is some code to exercise this example: 

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
    Dim i As Integer 
    Dim iArray() As Integer = New Integer() {6, 2, 4, 9} 
    ' The code below tells us that we have created a generic sort function thus eliminating the need of having multiple sort function to do different sorts. 
    BubbleSort(AddressOf SortAscending, iArray)           
    For i = 0 To 3 
    Debug.WriteLine(CStr(iArray(i))) 
    Next 
    Debug.WriteLine 
    BubbleSort(AddressOf SortDescending, iArray) 
    For i = 0 To 3 
    Debug.WriteLine(CStr(iArray(i))) 
    Next 
End Sub 

那么这个例子的RaiseEvent走过这混淆了我,那是假设是?:

Module Module1 

    Dim WithEvents ValueInfo As New Value() 

    Class Value 
     Public Event ValueUp(ByVal Amount As Double) 
     Public Event ValueDown(ByVal Amount As Double) 
     Public Event Result(ByVal Amount As Double, ByVal AnnounceDate As DateTime) 

     Public Sub GenerateEvents() 
      RaiseEvent ValueUp(2) 
      RaiseEvent ValueDown(-5.5) 
      RaiseEvent Result(1.25, Now()) 
     End Sub 
    End Class 

    Sub PriceGoingUp(ByVal Price As Double) 
     Console.WriteLine("Up: " & Price) 
    End Sub 

    Sub PriceGoingDown(ByVal Price As Double) 
     Console.WriteLine("Down: " & Price) 
    End Sub 

    Sub ResultAnnouncement(ByVal Amount As Double, ByVal AnnounceDate As DateTime) 
     Console.WriteLine("Result: " & Amount & " " & AnnounceDate) 
    End Sub 


    Sub Main() 
     AddHandler ValueInfo.ValueUp, AddressOf PriceGoingUp 
     AddHandler ValueInfo.ValueDown, AddressOf PriceGoingDown 
     AddHandler ValueInfo.Result, AddressOf ResultAnnouncement 

     ValueInfo.GenerateEvents() 
    End Sub 

End Module 

上面的例子中替代我将不胜感激,如果有人可以提供解释。谢谢!

回答

0

我知道描述差异的最简单方法就是这样。

  • 委托是对可以使用与目标方法相同的签名进行调用的单个方法或方法链的引用。
  • 事件是向代理添加和删除方法引用的机制的封装。

这里有一个重要的区别。事件不是代表。这是使用委托来实现观察者模式的.NET方式,它禁止事件订阅者滥用委托链。

RaiseEvent是VB通过事件机制调用委托的方式。请记住,委托可以引用多个方法,因此调用委托可能会导致执行多个方法。

您可以阅读此article以获得更详细的解释。