Saturday, July 13, 2013

VBScript - ByVal Vs ByRef / VBScript Passing Values ByRef or ByVal

Let Us See how we can get values by Reference and by Values :

In VBScript ByRef and ByVal:

<%
Function MyFunctionByRef(ByRef Myvalue)
      Myvalue = Myvalue + 1
End Function

Dim sendvalues : sendvalues = 3
Dim MyOutput: MyOutput = MyFunctionByRef(sendvalues)
Response.write "MyOutput: " & MyOutput  '4
%>

<%
Function MyFunctionByVal(ByVal Myvalue)
     Myvalue = Myvalue + 1
End Function

Dim sendvalues : sendvalues = 3
Dim MyOutput: MyOutput = MyFunctionByVal(sendvalues)
Response.write "MyOutput: " & MyOutput '4
%>



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