摘要:(200-06-28) VB.NET 委派(Delegate)
委派(Delegate)
定義 Method 結構
定義
'定義委派
Public Delegate Sub sayHello(ByVal thing As String)
主程式
Imports mod08
Module TestHelloWorldDelegate
Public Sub Main()
'建構委派
Dim handler As New sayHello(AddressOf engHelloWorld)
'呼喚(喚醒)-同步
handler.Invoke("eric")
End Sub
'給委派用的程序
Private Sub engHelloWorld(ByVal s As String)
System.Console.WriteLine(s + " Hello World!!!")
End Sub
End Module
類別層級
主程式 應用程式層級 (Event)
Public Class HelloWorld
'Data field
Private _msg As String
'事件
Public Event myhandler As sayHello
'屬性
Public Property Msg() As String
Get
Return Me._msg
End Get
Set(ByVal value As String)
Me._msg = value
End Set
End Property
'引發事件的 method
Public Sub Rasing(ByVal s As String)
'引發事件->牽涉到應用系統所寫的事件程序(???)
RaiseEvent myhandler(s)
End Sub
End Class
Module TestClassEvent
Public Sub Main()
'建構物件
Dim hello As New HelloWorld()
'建立事件程序
'管理員建立
Dim handler As New sayHello(AddressOf HelloWorld)
'聽物件的事件
AddHandler hello.myhandler, handler
'引發
hello.Rasing("eric")
End Sub
'事件程序
Private Sub HelloWorld(ByVal s As String)
System.Console.WriteLine(s + " 世界和平!!")
End Sub
End Module