委托三个步骤
1、声明委托 用Delegate 声明一个委托 类型 参数要和 被委托的方法一样 例如 Delegate Function a(byval x as string) as string
2、实例化委托 dim t as new a(AddressOf Function Name)
3.通过 t(参数) 或者 t.Invoke(参数调用委托)
示例:
Module module1
Delegate Function a(ByVal x As Integer, ByVal y As Integer) As Integer '声明委托类型 委托可以使一个对象调用另一个对象的方法
Function sum(ByVal x As Integer, ByVal y As Integer) As Integer
Return (x + y)
End Function
Sub main()
Dim d As New a(AddressOf sum) '实例化委托
Dim s = 0
s = d.Invoke(1, 2) '执行委托
Console.WriteLine(s.ToString())
s = d(1, 2) '执行委托
Console.WriteLine(s.ToString())
MsgBox("")
End Sub
End Module
Public form2 As Form = Form1
Public Sub CALLBACK_Timer(id As UInt32, _
msg As UInt32, _
ByRef userCtx As UInt32, _
rsv1 As UInt32, _
rsv2 As UInt32)
mCount += 1
Dim deli As New myDelegate(AddressOf SetWindowText)
form2.Invoke(deli, mCount)
' deli.invoke(mCount)
End Sub