VB.NET 中的委托与事件一例
程序员文章站
2022-06-16 10:26:49
...
Namespace Events
Public Delegate Sub NumberReachedEventHandler(ByVal sender As Object, ByVal e As NumberReachedEventArgs)
Public Class Counter
Public Event NumberReached As NumberReachedEventHandler
Public Sub New()
End Sub
Public Sub CountTo(ByVal countTo As Integer, ByVal reachableNum As Integer)
If countTo < reachableNum Then Throw New ArgumentException("reachableNum should be less than countTo")
For ctr As Integer = 0 To countTo
If ctr = reachableNum Then
Dim e As NumberReachedEventArgs = New NumberReachedEventArgs(reachableNum)
OnNumberReached(e)
Return
End If
Next
End Sub
Protected Overridable Sub OnNumberReached(ByVal e As NumberReachedEventArgs)
RaiseEvent NumberReached(Me, e)
End Sub
End Class
Public Class NumberReachedEventArgs
Inherits EventArgs
Private _reached As Integer
Public Sub New(ByVal num As Integer)
Me._reached = num
End Sub
Public ReadOnly Property ReachedNumber As Integer
Get
Return _reached
End Get
End Property
End Class
End Namespace
此代码取自CODEPROJECT.COM,放在这里,仅供学习使用
上一篇: js中的事件委托或是事件代理详解
下一篇: 委托与事件(4)