Understanding Procedures in VBScript - More on Scopes and Parameters
(Page 4 of 5 )
In the last example, I passed the intBorn variable as a parameter which made it available in a new scope. This is known as passing a variable “by reference.”
When a script runs, the script host stores variables in system memory. It creates a “reference” to the memory address where it stores the “value.” The variable name that we assign is actually a pointer to that reference.
When we pass that variable as a parameter, it is passed by reference, which means that the new scope will use the same memory reference. In other words, if we were to change the variable's value in the new scope, those changes would also appear in the original scope as well.
PrintMessage
Sub PrintMessage
strMessage = " This is my message. "
WScript.Echo strMessage
Call CleanString(strMessage)
WScript.Echo strMessage
End Sub
Sub CleanString(strMessage)
strMessage = Trim(strMessage)
End Sub
Take for instance this example. It simply takes a string and removes any leading and trailing white spaces, and then displays the resulting string. You can see when you run this script that the value of the strMessage variable is changed by the CleanString subroutine. This is because the variable was passed “by reference.”
To better understand this concept, let’s take a look at its converse. The opposite of passing a variable by reference is passing a variable “by value.” When passing a variable by value, the value is maintained while a new reference is created in memory. This is used to preserve the original value of a variable.
PrintMessage
Sub PrintMessage
strMessage = " This is my message. "
WScript.Echo strMessage
Call CleanString(strMessage)
WScript.Echo strMessage
End Sub
Sub CleanString(ByVal strMessage)
strMessage = Trim(strMessage)
WScript.Echo strMessage
End Sub
Look again at my Sub statement. Notice that I’ve included the ByVal keyword to instruct VBScript to pass strMessage by value instead. This preserves the original value of strMessage in the PrintMessage scope by creating a duplicate reference in memory. One is used in the PrintMessage scope and the other is used within the CleanString scope. I’ve added some extra Echo statements so that you can see the value of the variables within each of the different scopes.
There is also a ByRef keyword to specify passing a variable by reference. However, it is unnecessary in VBScript since this is the default behavior.
What about VBScript’s naming conventions? You’ve been told that you cannot have two different variables with the same name, right? In VBScript, two variables may have the same name as long as they are created and used in the context of different scopes.
The second (and probably more common) use of parameters is to provide flexibility for your script by performing a set of predefined actions on different sets of data.
Call PrintMessage("This is the first message.")
Call PrintMessage("This is the second message.")
Sub PrintMessage(strMessage)
WScript.Echo strMessage
End Sub
In this example, I’m using parameters to provide different data to my subroutine each time it runs.
Next: Final Concepts >>
More Windows Scripting Articles
More By Nilpo