We help IT Professionals succeed at work.

Different sub statements

Frank Freese
Frank Freese asked
In my learning VB when I create a button the following code is created:
 Private Sub Button1_Click(sender As System.Object, 
e As System.EventArgs) Handles Button1.Click
 End Sub

Open in new window

However, when I look at the exmaples from the book I'm using I see this
 Private Sub Button1_Click(ByVaL sender As System.Object,
 ByVal e As System.EventArgs) Handles Button1.Click
 End Sub

Open in new window

The book adds the ByVal. Do I need to change some default(s)?
Watch Question

Solutions Consultant
By default variables are passed by value so both of the above statements do the same thing

So the correct answer is whichever works best for you

Here is a link on passing variables you may find useful




ok...if I neded ByRef then would I need to include both ByValue and ByRef or just ByRef?
Michael FowlerSolutions Consultant

If you do not include ByVal it is assumed by the system. If you want to use ByRef then you need to specify it. You cannot add both. To specify ByRef you would use something like

Private Sub (ByRef myVariable as string)

Note - Understanding reference and value types can be very important, especially when dealing with objects

Here is good discussion

The difference between value types and reference types, in the most basic form, is that value types hold their data directly, while reference types point to a location in memory, and the data is located in that memory.



thank you