I have seen ByVal used so many times, but what is it and whats the purpose of it?
Printable View
I have seen ByVal used so many times, but what is it and whats the purpose of it?
It is more efficient to pass things ByVal then ByRef but the real difference is that ByRef allows a variable passed to it to be changed in the receiving sub whereas ByVal doesn't.
Taken from: http://www.developerfusion.co.uk/show/51/6/
No need to reinvent the wheel. :cool:
When getting the value of a variable from a procedure or function parameter you have two ways to request it. Either you can request for it to be passed ByRef (default), or ByVal.
Passing ByVal
VB Code:
Private Sub TestSub(ByVal strPath As String) Passing ByRef Private Sub TestSub(ByRef strPath As String) ' or just Private Sub TestSub(strPath As String)
When you pass ByVal, Visual Basic passes a copy of the variable to the procedure. This way the procedure gets the value, but any changes it makes will not affect the original variable.
When you pass ByRef, Visual Basic passes a pointer to the procedure. This is a reference so that the procedure knows where to find the variable in the memory. Any changes the procedure makes to this variable will effect the original one, as they are the same thing, however the variable does not need to be declared as public if you were wanting the procedure to access the variable any other way.
The following example shows the differences:
VB Code:
Sub Form_Load() Dim strTest As String '// fill the variable strTest = "Hello from Form_Load" '// call the procedure Call TestSub(strTest) '// display a message box containing the value of strTest Msgbox strTest End Sub '// TestSub procedure when passing ByVal Sub TestSub(ByVal strString As String) strString = "Hello from TestSub" '// when control returns to Form_Load, no changes will have '// been made to strTest End Sub '// TestSub procedure when passing ByRef Sub TestSub(ByRef strString As String) strString = "Hello from TestSub" '// when control returns to Form_Load, the value of '// strTest will have changed to Hello from TestSub End Sub
Note that when you are passing a variable ByRef, it must be declared as a specific datatype (ie string). Otherwise, VB cannot pass a pointer to it. If you do not do this, you will get a compile error:
ByRef Argument Type Mismatch.
This does not occur when passing ByVal. For example, the code below will produce a compile error when you press Command1:
VB Code:
Private Function TestFunction(ByRef sString As String) sString = sString & vbCrLf End Function Private Sub Command1_Click() Dim sTestString TestFunction sTestString End Sub
while, the code below would not, as you have explicitly declared sTestString As String.
VB Code:
Private Function TestFunction(ByRef sString As String) sString = sString & vbCrLf End Function Private Sub Command2_Click() Dim sTestString As String TestFunction sTestString End Sub
So the initial value of ByVal cannot be modified but ByRef can be modified?
Exactly. As was mentioned in MWagner's post, ByVal just passes the value, whereas ByRef passes a reference (an address in memory) so that the variable can be changed.Quote:
Originally Posted by BrailleSchool
ByVal is just the value. If I tell you I have $5.28 in my pocket, you can't change it. If I give you the $5.28 to count for yourself, you can change it.
Since a sub has the ByRef variable itself, not just its value, it can change it. You're giving it the actual variable (by giving it the address of that variable), not just telling it what the variable is worth.
Note that using ByRef makes it easy to pass back two or more values from a Function or even a Sub. Here is a trivial example. You could add more ByRef parameters if needed.
VB Code:
Public Function xxx(ByRef x As Integer) As Boolean x = x + 1 xxx = True End Function
If I read that right, xxx is a boolean flag and x is a number that being incremented by 1. x can be changed because its a ByRef. So if it was ByVal x As Integer, x = x + 1 would then bring an error?Quote:
Originally Posted by MartinLiss
also, if i understood this thread correctly,
would be the same asVB Code:
Public Function xxx(ByRef x As Integer) As Boolean x = x + 1 xxx = True End Function
VB Code:
Public Function xxx(x As Integer) As Boolean x = x + 1 xxx = True End Function
Yes since ByRef is the default.
Ok, thanks for your help in understanding this guys.