Variables are named storage locations with values that can change. Variable names can contain letters, numbers, and underscore characters ( _ ), but must start with a letter. These names are not case-sensitive and can be no longer than 255 characters.
Open Text recommends that you give your variables meaningful descriptive names; for example, variables named count, contents, and defaultValue are more meaningful than variables named x, y, and z.
You cannot give your variables names corresponding to reserved words. Examples of reserved words include:
For a complete listing of OScript reserved words, see Reserved Words.
Any of these reserved words can be used as part of your variable name (for example, openPage and ifExists are valid variable names).
A variable declaration associates a data type (for example: Boolean, File, String) with a variable. The declared variable can then assume a value of the specified type, Undefined, or Error.
A variable declaration consists of a data type name followed by one or more spaces and/or tabs and a list (separated by commas) of one or more variable names of that type. A typical set of variable declarations might look like this:
Integer numberOfItems
Boolean found, modified
File libraryDoc, externalDoc
Variables should be declared before they are used. If you do not declare a variable, the OScript compiler implicitly assumes it to be Dynamic, allowing its type to change. The compiler performs no type checking on Dynamic variables.
Variables can be initialized in their declaration. To specify an initial value in your variable declaration, follow the variable name with the OScript assignment operator and an initial value. For example:
Integer numberOfItems = 6
Date rightNow = Date.Now()
String strDate = Str.String( rightNow )
If you attempt to assign a value to an undeclared variable, a warning message is generated when you compile the script, indicating that the variable has not been declared. If you do not correct this error, the compiler assumes you intended to create a new Dynamic local variable.
Variable declarations can be placed anywhere in your script but are commonly found at the beginning of the script, making it easier to read.
The scope of a variable defines the range over which it is accessible. OScript allows you to define both local variables and global variables, each with a different scope.
Local variables are defined only within the script, function, or control statement in which they are declared.
For example, if your script contains a call to another script:
Integer result
result = CalledScript()
The variable result, defined in the calling script, is not accessible to CalledScript.
Or, for example:
Integer myNumber
String myText
myText = subFunction()
function void subFunction()
...block of code...
end
References to myNumber or myText from within the function subFunction are invalid since they are defined outside of subFunction. For more information about functions and how they are defined, see the Functions section.
Or, for example:
Integer myNumber = 1
while ( myNumber > 20 )
Integer yourNumber = myNumber * 10
myNumber += 1
end
References to yourNumber outside of the while loop are invalid since yourNumber was declared inside the loop.
A global variable is accessible by all scripts. A global variable is not explicitly declared. It is created the first time a value is assigned to it, and exists for the duration of a program.
Global variable names are prefaced with a dollar sign ($).
Integer myAge
$globalAge = 25
myAge = $globalAge + 10
There is no compile-time checking of either the type or existence of global variables. Exercise caution when using global variables.