總網頁瀏覽量

2011年10月14日 星期五

[VB.NET] 事件(Event)

'定義委派
Public Delegate Sub sayHello(ByVal thing As String)

Public Class HelloWorld
    Private _msg As String
    '事件
    Public Event MyEvent As sayHello
    '屬性
    Public Property Msg() As String
        Get
            Return _msg
        End Get
        Set(ByVal value As String)
            _msg = value
        End Set
    End Property


    '引發事件的 method
    Public Sub REmethod(ByVal s As String)
        '引發事件
        RaiseEvent MyEvent(s)
    End Sub
End Class

Module TestEvent

    Public Sub Main()
        '建構物件
        Dim hello As New HelloWorld()
        '建立事件程序
        '管理員建立
        Dim handler As New sayHello(AddressOf HelloWorld)
        '聽物件的事件
        AddHandler hello.MyEvent, handler
        '引發
        hello.REmethod("cloud")
    End Sub

    '事件程序
    Private Sub HelloWorld(ByVal s As String)
        System.Console.WriteLine(s + " 歡迎光臨!!")
    End Sub
End Module

沒有留言:

張貼留言