1

Please have a look at the code below:

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

The output is:

1
DelegateTest2
DelegateTest3

I do not understand why this application compiles i.e. the delegate accepts an integer in its signature but Person.DelegateTest2 and Person.DelegateTest3 do not.

If I change Person.DelegateTest2() to the following then I do get an error as I would expect:

Public Sub DelegateTest2(ByVal Test As Integer, ByVal Test2 As Integer)
        MsgBox("Delegate Test 2")
    End Sub

Why does the Delegate allow you to pass zero arguments when it has arguments i.e. an integer in my case?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
w0051977
  • 15,099
  • 32
  • 152
  • 329
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 17 '13 at 12:35
  • @John Saunders, thanks. Point noted. – w0051977 Feb 17 '13 at 12:36

1 Answers1

2

Don't forget that VB.NET inherits all the legacy baggage from the beloved VB. You could make it strict by putting the following to the top of your file so that it behaves as a real .NET programming language and not some hybrid crap:

Option Strict On

Also I would recommend you setting this to be the default option so that you don't find yourself in the wilderness.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks. Why does it still allow you to pass zero arguements if option Strict is Off? – w0051977 Feb 17 '13 at 12:46
  • Man I have strictly no idea. I guess it's one of those *VB.NET thingies*. VB.NET also also you to use weak typing because that's how it was in VB. Have you looked at the generated IL? I have and it's a real mess :-) Really, use the `Strict On` option if you want to be doing some .NET development using this language. Otherwise C#. – Darin Dimitrov Feb 17 '13 at 12:47
  • @w0051977 - Recent versions of VB.Net added a feature called "Relaxed Delegates". I'm not sure if that will explain the behavior you are seeing, but you might look it up and see. – Chris Dunaway Feb 18 '13 at 15:40