ByVal
Declaration specifier to explicitly pass a parameter by value
ByVal in a parameter list of a declare statement causes a copy of the variable to be passed to the procedure (for example, a sub or function) by its value.
This means that if the value of the variable x is passed, then the original variable x will not be modified in any way; however, if the variable were passed ByRef, the value of the original variable x could be modified by the called function.
Opposite of ByRef.
The ByVal keyword is also used in the context of Byref Parameters and Function Results, where it can be used to explicitly override the by-reference semantics in order to pass or assign a pointer as-is to a Byref parameter or function result. For reference:
Syntax
Usage
Description
ByVal in a parameter list of a declare statement causes a copy of the variable to be passed to the procedure (for example, a sub or function) by its value.
This means that if the value of the variable x is passed, then the original variable x will not be modified in any way; however, if the variable were passed ByRef, the value of the original variable x could be modified by the called function.
Opposite of ByRef.
The ByVal keyword is also used in the context of Byref Parameters and Function Results, where it can be used to explicitly override the by-reference semantics in order to pass or assign a pointer as-is to a Byref parameter or function result. For reference:
Example
Sub MySub(ByVal value As Integer)
value += 1
End Sub
Dim MyVar As Integer
MyVar = 1
Print "MyVar: "; MyVar 'output = 1
MySub MyVar
Print "MyVar: "; MyVar 'output = 1, because byval won't change the values passed into it globally.
Sleep
End
value += 1
End Sub
Dim MyVar As Integer
MyVar = 1
Print "MyVar: "; MyVar 'output = 1
MySub MyVar
Print "MyVar: "; MyVar 'output = 1, because byval won't change the values passed into it globally.
Sleep
End
Dialect Differences
Differences from QB
- QB only used ByVal in declarations to non-Basic subroutines
See also