0

I have programmed in VB.NET for a while now and have never known what the

ByVal sender As System.Object, ByVal e As System.EventArgs

parameters in the functions created automatically by the designer to handle events on your forms.

I have recently discovered that they can be removed, similarly to VB classic. I think it makes the code much more readable, and if they can be removed, then why are they there is the first place. Can it be set where they aren't inserted in the first place?

P.S. I am using Visual Basic 2010 Express

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jason Mills
  • 585
  • 2
  • 6
  • 20
  • Possible duplicate of [VB Byval I don't need it](http://stackoverflow.com/questions/2395894/vb-byval-i-dont-need-it)? – PakkuDon Feb 22 '14 at 04:13
  • try this answer: http://stackoverflow.com/questions/1303145/net-events-what-are-object-sender-eventargs-e – J King Feb 22 '14 at 04:15
  • @PakkuDon I'm wondering why the parameters need to be inserted in the first place since removing them seems to have no effect on the end result. – Jason Mills Feb 22 '14 at 04:23
  • Those objects are needed if you need to use them, simply as that, for example, in a eventhandler, they are so basic, read a .NET tutorial. – ElektroStudios Feb 22 '14 at 08:31

3 Answers3

2

The sender parameter tells you what the source control of the event was, while the e parameter may give you additional information about the event. If you never use e, and never have more than one control firing the same event handler, then you'll never see a difference.

Without the parameters, you can't do this:

Private Sub Button1_Click() Handles Button1.Click, Button2.Click
    Dim btn As Button = DirectCast(sender, Button)
    MessageBox.Show(btn.Text)
End Sub

Or possibly this:

Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Right Then
        MessageBox.Show("Right Button Down")
    End If
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
1

The "what are they for" question is a duplicate, and has been answered by Idle_Mind.

The feature of handling events with no arguments was added (in VS2008 I think), but it is implemented by simply creating the "missing" call, so

Sub Button1_Click() Handles Button1.Click

is implemented as

Sub Lambda1(ByVal s As Object, ByVal e As EventArgs) Handles Button1.Click
  Button1_Click()
End Sub

Sub Button1_Click()

I avoid the feature because of the extra calls in the callstack, compared to the "simplicity" of just ignoring parameters you don't use.

Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
1

Your question indicates that there is a deeper problem with your VS options.

When the event handler is called, those parameters are passed to it. You really should have method calls complying with the method signature.

If you use Option Strict On then VS will tell you that something is not right. I strongly recommend setting On to be the default for new projects, and that every time you install VS that that is one of the first things you do.

If you don't have the methods and their calls matched up exactly, then with Option Strict Off the compiler has to guess what you meant and fiddle around to try to get something that will run. Sometimes it will guess correctly, sometimes it won't.

You might want to call a method like an eventhandler without specifying the sender or the eventargs, you can do that with

Button1_Click(Nothing, EventArgs.Empty)

although in that case it would usually be cleaner to have the event handler call a method which you can call separately:

Not-so-good:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' a load of code here
End Sub

Private Sub DoSomething()
    ' some code
    Button1_Click(Nothing, EventArgs.Empty)
    ' some more code
End Sub

Better:

Private Sub DoStuff()
    ' a load of code here
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    DoStuff()
End Sub

Private Sub DoSomething()
    ' some code
    DoStuff()
    ' some more code
End Sub

(My code does not show the ByVal because I used VS2013 Express to write it, which doesn't put it in because ByVal is the default.)

When you set Option Strict On, you may find that you get a lot of type-mismatch errors. Do not be disheartened: go through them one at a time to make sure all your variable types match up correctly. VS will even suggest some corrections which it can make automatically for you; take the time to look at the suggestions rather than blithely accepting them as it only gets it right about 99.9% of the time, and in some cases other slight changes to your code would obviate the need for the suggested change.

Edit: as Mark Hurd pointed out in his answer, VS will automatically make up for the incorrect method signature. Like him, I cannot recommend taking advantage of that.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • @MarkHurd That's disappointing to find out, almost on par with option strict off still being the default for new installations of VS. – Andrew Morton Feb 23 '14 at 19:12