对事件感到困惑

时间:2010-08-26 08:41:44

标签: vb.net

我对Raise Events示例和使用委托作为函数指针非常困惑:

我很清楚下面的这个函数指针示例程序创建了一个泛型排序函数,用于排序整数数组而不改变主排序函数。

' 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

如果有人能提供解释,我将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

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

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

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

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

您可以阅读此article以获取更详细的说明。