委託可以認爲是一類方法簽名一致的方法抽象類。
委託對象必須由簽名一致的方法實例化,而通過調用委託對象可以調用實例化委託對象的方法。
Public Class Form1
Public Delegate Function addMethod(ByVal x, ByVal y) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As addMethod
Dim b As Tdelegate
b = New Tdelegate()
a = New addMethod(AddressOf b.add)
'TextBox1.Text = TextBox1.Text + a(3, 9).ToString
'TextBox1.Text = TextBox1.Text + (New addMethod(AddressOf New Tdelegate().add))(3, 9).ToString
TextBox1.Text = TextBox1.Text + New addMethod(AddressOf New Tdelegate().add).Invoke(3, 9).ToString
End Sub
End Class
Public Class Tdelegate
Public Function add(ByVal x, ByVal y) As Integer
Return x + y
End Function
End Class