欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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,放在这里,仅供学习使用

相关标签: 事件 委托