更新:2007 年 11 月
此示例演示如何将方法与委托关联然后通过委托调用该方法。
创建委托和匹配过程
- 创建一个名为 MySubDelegate 的委托。 - Delegate Sub MySubDelegate(ByVal x As Integer)
- 声明一个类,该类包含与该委托具有相同签名的方法。 - Class class1 Sub Sub1(ByVal x As Integer) MsgBox("The value of x is: " & CStr(x)) End Sub End Class
- 定义一个方法,该方法创建该委托的实例并通过调用内置的 Invoke 方法调用与该委托关联的方法。 - Protected Sub DelegateTest() Dim c1 As New class1 ' Create an instance of the delegate. Dim msd As MySubDelegate = AddressOf c1.Sub1 ' Call the method. msd.Invoke(10) End Sub