|
-
Apr 27th, 2010, 01:59 PM
#1
ByVal vs ByRef, and Value types vs Reference types
Hey,
This is an 'article' about two concepts:
- Passing arguments ByVal or ByRef
- Value types vs Reference types
Since this seems to be a common source of issues for both beginners and more experienced VB.NET programmers, I decided to write a little codebank article on it. This is aimed primarily at the beginners, and the details may not be absolutely correct. I simplified some concepts to make it easier to understand, so beginners can use the concepts correctly without having to know everything about memory management in .NET 
A little disclaimer: I don't pretend to know everything about the subject, but hopefully enough to write this article. If I have something wrong, please feel free to correct me 
Passing arguments ByVal or ByRef
In VB.NET, every argument that a method takes is preceded by either the ByVal (which is the default) or ByRef keyword. Which keyword you use decides how VB will handle the object you pass as the argument when you call the method.
Code:
Private Sub Sum(ByVal x As Integer, ByVal y As Integer)
Dim result As Integer = x + y
MessageBox.Show(result.ToString)
End Sub
The keyword ByVal stands for 'by value'. It tells VB that the value passed into this argument should be passed 'by value'. In other words, VB makes a local copy of the object, one that is completely separate from the object you pass into the method.
Because of this, any change you make to an argument passed by value will not persist beyond that method. A simple example:
Code:
Private Sub ByValMethod(ByVal x As Integer)
x = 5
End Sub
If you call this method, passing some value for x (let's say 3), then that value will still be 3 after the method, and not 5!
Code:
Dim value As Integer = 3
ByValMethod(value)
MessageBox.Show(value.ToString)
The message box will show "3", and not "5", even though you explicitly set the argument 'x' to 5 in the method ByValMethod!
Why does this happen? It's very simple: the variable 'x', the argument, is a local copy of the variable that is passed to the method ('value'). A local copy is made, and that copy gets the same value (3). Then, you change the value of the local copy to 5, but you never touch the original variable, and the variable 'value' will remain 3. The local copy, now equal to 5, is 'lost' after the ByValMethod method is done (the variable goes "out of scope" and its value is gone).
So, what do you do if you do want the original variable to be changed? You use ByRef!
ByRef stands for 'by reference'. It tells VB that the argument passed to a function is to be passed by its reference.
You must understand that every variable in VB is located somewhere in the computer's memory. For example, the value of the variable 'value' in the code sample above could, possibly, be stored at memory location 12385 (or something). VB will simply tell the computer to look up the value at memory location 12385, and it will see that the memory contains the value 3.
When you pass an argument by reference, VB actually passes this memory location to the method.
Let's consider the same method as before, but now we pass the argument by reference:
Code:
Private Sub ByRefMethod(ByRef x As Integer)
x = 5
End Sub
The variable 'x' now points to the same memory location of the object that you pass into this method.
So, if we use the same code as before, except we now call the ByRefMethod,
Code:
Dim value As Integer = 3
ByRefMethod(value)
MessageBox.Show(value.ToString)
the variable 'x' in the method ByRefMethod now points to the same memory location as the variable 'value'.
So, the method will set the value of that memory location to 5, and the value of the variable 'value' is actually changed to 5 this time! The message box will thus show "5", as you might have expected it would always do.
So, in short: when you want to change the value of an argument inside a method, that argument must be passed by reference (ByRef), because otherwise you are only changing a local copy of the variable, which won't do you any good outside of the method it lives in.
However! There is an important "but" here. Before I explain what it is though, you need to understand the difference between reference types and value types, so read on to the next post!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|