We help IT Professionals succeed at work.

Different sub statements

Frank Freese
Frank Freese asked
on
Experts,
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)?
Comment
Watch Question

Solutions Consultant
Commented:
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

http://msdn.microsoft.com/en-us/library/ddck1z30.aspx

Michael

Author

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

Commented:
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
http://www.vbforums.com/archive/index.php/t-612831.html

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.

Michael

Author

Commented:
thank you