Scope and Lifetime Of Variables
A variable's scope is determined by where you declare it. When you declare a variable within a procedure, only code within that procedure can access or change the value of that variable. It has local scope and is a procedure-level variable. If you declare a variable outside a procedure, you make it recognizable to all the procedures in your script. This is a script-level variable, and it has script-level scope.
The lifetime of a variable depends on how long it exists. The lifetime of a script-level variable extends from the time it is declared until the time the script is finished running. At procedure level, a variable exists only as long as you are in the procedure. When the procedure exits, the variable is destroyed. Local variables are ideal as temporary storage space when a procedure is executing.
The following examples illustrates the use of the BSL Variables:
Example 1: Global Scope
Dim GlobalVariable
GlobalVariable = "This is a script-level variable."
Sub MySub
GlobalVariable = "Modified within procedure."
End Sub
Example 2: Local Scope
Sub MySub
Dim LocalVariable
LocalVariable = "This is a local variable."
End Sub