Confused with Events
I am very confused over a Raise Events example and using delegate as a function pointer:
I am clear with this function pointer example program below which creates a generic sort function for sorting integer arrays without changing the main sort function.
' 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 I开发者_开发问答nteger( ) {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
Then this RaiseEvent example came along which confuses me, is it suppose to be an alternative to the example above?:
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
I would appreciate if someone can provide an explanation. Thanks!
The simplest way I know to describe the difference is like this.
- A delegate is a reference to a single method or chain of methods that is callable using the same signature as the target method.
- An event is an encapsulation of the mechanism for adding and removing method references to a delegate.
There is an important distinction to be made here. An event is not a delegate. It is the .NET way of implementing the observer pattern using delegates in a manner that prohibits event subscribers from mucking up the delegate chain.
RaiseEvent
is VB's way of invoking the delegate through the event mechanism. Remember, delegates can reference more than one method so invoking a delegate could cause more than one method to execute.
You can read this article for a more detailed explanation.
精彩评论