Wednesday, March 21, 2012

VBScript - ByVal Vs ByRef



VBScript - ByVal Vs ByRef




Passing Parameters ByRef and ByVal

In VBScript, there are two ways values can be passed:
1) ByVal

2) ByRef

Using ByVal, we can pass arguments as values whereas with the use of ByRef, we can pass arguments are references.
ByVal:

the parameter is passed by value, thus the subroutine works with a copy of the original.
ByRef:

the parameter is passed by reference, thus the subroutine works directly with the original.


Example: Parameters By Value


Function GetValue( ByVal var )
var = var + 1
End Function

Dim x: x = 5

'Pass variable x to GetValue function ByVal
GetValue x

Response.write "x = " & x

Output: x = 5


Example: Parameters By Reference:


Function GetReference( ByRef var )
var = var + 1
End Function

Dim x: x = 5

'Pass the variable x to the GetReference function ByRef
GetReference x

Response.write x

Output: 6


No comments:

Post a Comment