請看看下面的代碼:代表參數是否需要匹配事件簽名?
Public Delegate Sub TestButtonClick(ByVal test As Integer)
Public Class Person
Private Name As String
Private ID As Integer
Public Event ButtonClick As TestButtonClick
Public Sub DelegateTest1(ByVal Test As Integer)
MsgBox(Test)
End Sub
Public Sub ChangeName()
RaiseEvent ButtonClick(1)
End Sub
Public Sub DelegateTest2()
MsgBox("Delegate Test 2")
End Sub
Public Sub DelegateTest3()
MsgBox("Delegate Test 3")
End Sub
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim p1 As Person = New Person
AddHandler p1.ButtonClick, AddressOf p1.DelegateTest1
AddHandler p1.ButtonClick, AddressOf p1.DelegateTest2
AddHandler p1.ButtonClick, AddressOf p1.DelegateTest3
p1.ChangeName()
End Sub
End Class
輸出是:
1
DelegateTest2
DelegateTest3
我不明白爲什麼這個應用程序編譯即代表接受其簽名,但Person.DelegateTest2
和Person.DelegateTest3
整數不要。
如果我改變Person.DelegateTest2()來那麼下面我一樣我希望得到一個錯誤:
Public Sub DelegateTest2(ByVal Test As Integer, ByVal Test2 As Integer)
MsgBox("Delegate Test 2")
End Sub
爲什麼委派允許您傳遞零個參數時有爭論,即一個整數在我的情況?
我編輯了你的標題。請參見「[應的問題包括‘標籤’,在他們的頭銜?(http://meta.stackexchange.com/questions/19190/)」,這裏的共識是「不,他們不應該」。 – 2013-02-17 12:35:04
@John Saunders,謝謝。 Point指出。 – w0051977 2013-02-17 12:36:11