Whats A 'Byval' and how do I
benifit by using it?
Printable View
Whats A 'Byval' and how do I
benifit by using it?
ByVal means that you are sending a value instead of a pointer to a memory address that contains the value.
Basically, you benefit by it in that if you pass a variable to a function ByVal (by value) then the function only recieves the value of the variable. It can then do whatever it wants to the value (add/subtract to it) yet the variable you passed remains unchanged.
ByVal's companion is ByRef (by reference), which is the default way of passing arguments. When you pass a value ByRef, if the function manipulates that variable, then that variable's value is changed.
Maybe this explains it better:
Add a CommandButton to a form (Command1) and paste this code into the form's code window:
P.S. If you are wondering about my naming convention, it's what we use at work.Code:Option Explicit
Private Sub Command1_Click()
Dim llNumber As Long
llNumber = 5
' Display the beginning value of llNumber
MsgBox "Starting value: " & llNumber
MultiplyByTwo llNumber
' llNumber has not changed.
MsgBox "After MultiplyByTwo: " & llNumber
MultByTwo llNumber
' Notice that now llNumber has actually been modified.
MsgBox "After MultByTwo: " & llNumber
End Sub
Private Sub MultByTwo(alNumber As Long)
' This function will actually change the value in the memory
' location pointed to by the variable name that is passed.
alNumber = alNumber * 2
MsgBox "Inside MultByTwo: " & alNumber ' Display the value inside this function
End Sub
Private Sub MultiplyByTwo(ByVal alNumber As Long)
' This function only manipulates the value of what is passed in,
' But not the value stored in memory that the variable name points to.
alNumber = alNumber * 2
MsgBox "Inside MultiplyByTwo: " & alNumber ' Display the value inside this function
End Sub
al = ArgumentLong
ll = LocalLong
[Edited by seaweed on 11-08-2000 at 08:42 PM]
Byval means that rather than passing a reference to a parameter VB passes a copy of of a parameter to a function, for example
Code:
Private Sub Command1_Click()
Dim x As Integer
x = 10
Call MyProc(x)
MsgBox x
End Sub
Private Sub MyProc(param As Integer)
param = param + 1
End Sub
would result in a messagebox with 11 written in it because the sub Myproc increments the value of x.
if we replace the definition of MyProc with
Code:Private Sub MyProc(ByVal param As Integer)
param = param + 1
End Sub
the result would be 10 because x is copied and then passed to myProc, it might seem that this limits your options rather than increasing them, but it does give you an extra variable you can play with if you need it, although as a rule it's more important to understand the difference between ByVal and ByRef (ByRef is the Default, it means pass a reference to the variable rather than a copy of it) than to use it extensivly, accessing parameters passed ByVal is also very slightly faster than byref, although it takes longer to call the function if doubles or currencys are passed byval. UDTs and Arrays cannot be passed byval, because it takes too long to copy them.
I hope this explains it a bit, it's a wierd concept to get your head round, and there are lots of people who are much beter at explaining things than me.
Thankyou very much sir!
That brings a whole new light to the
debuging of my programing because I
would pass values and then think that
they were not changing and they were!
Thankyou so much!