Public Class TestClass Sub Test() Dim increment1 = Function(x) x + 1 '@ Console.WriteLine(increment1(1))
Console.WriteLine((Function(num As Integer) num + 1)(1)) '@-1行
Dim writeline1 = Sub(x) Console.WriteLine(x) 'A writeline1("Hello")
'@をDelegateで書いたもの Dim increment As New DelegateTest_TestClass Dim d1 As TestDelegateF = AddressOf increment.Method1 '@-D
Console.WriteLine(d1(1)) Dim d3 As SampleDelegateS = AddressOf increment.Method3 'A-D d3("Hello") End Sub End Class Public Delegate Function TestDelegateF(x As Integer) As Integer Public Delegate Sub TestDelegateS(x As String)
Public Class DelegateTest_TestClass Public Function Method1(x As Integer) As Integer Return x + 1 End Function Public Sub Method3(x As String) Console.WriteLine(x)
Dim increment1 = Function(x As Integer) x + 1 Dim increment1 As TestDelegateF = Function(x As Integer) x + 1 Dim increment1 As TestDelegateF = Function(x) x + 1 Dim increment1 As Func(Of Integer, Integer) = Function(x) x + 1
> Dim d3 As SampleDelegateS = AddressOf increment.Method3 'A-D > Public Delegate Sub TestDelegateS(x As String)
Dim method As Action = AddressOf Console.WriteLine
またイベントハンドラと同様、一つのデリゲートに複数のメソッドを割り当てることもできます。
Sub Main() Dim method = DirectCast(System.Delegate.Combine( New System.Action(Sub() System.Console.WriteLine("ABC")), New System.Action(AddressOf System.Console.WriteLine), New System.Action(Sub() System.Console.WriteLine("XYZ")), New System.Action(AddressOf Now.ToString), New System.Action(Sub() MsgBox("マルチキャストデリゲート"))), System.Action) method() End Sub