Author: Halfvares Mats, Teknikhuset AB.

Published: 2005-03-08

Applies to:
  • Content Studio ver. 3.x - 4.x 

Type: Information


When calling a function in VBScript all parameters are passed by reference by default something that can cause unexpected effects. In VB.NET this has been changed so that parameters are passed by value by default.

More information

If you have the following function that modifies its input parameters you can have different effects depending on the way you pass the parameters if the parameters are variables.
 Sub mysub(p1, p2)
  p1 = "hello"
  p2 = "good bye"  
 End Sub

By passing the parameters by value you send a copy of the value to the procedure and the values passed in variables will not be changed. There are several ways of doing this in VBScript (NOTE that the last line only works in Visual Basic, not in VBScript). By including a parameter in parentheses you tell the VB compiler that the variable should be passed by value. This is the default behaviour in VB.NET.

 'By value
 Dim s, d
 Call mySub((s), (d))
 mySub (s), (d)
 'This will not work i VB Script!!
 Call mySub(ByVal s, ByVal d)
Call mySub(ByVal s, ByVal d)
By passing a reference to the original variable the procedure will be able to change the value of the passed in variables. This is the default behaviour in VBScript.

'By reference
Dim s, d
Call mySub(s, d)
mySub s, d