Using Caché Basic
Variables
[Home] [Back] [Next]
InterSystems: The power behind what matters   
Class Reference   
Search:    

A variable is the name of a location in which a runtime value can be stored. There are three types of variables: local variables, process-private global variables, and global variables.

Variable Names
Each variable in Caché Basic has a name that uniquely identifies it within its level of scope. Variable names are case-sensitive. Variable names may be of any length, but must be unique within the first 31 characters (exclusive of prefix characters). Variables can have multiple levels of subscripts.
For further details on percent variables and system names, see Rules and Guidelines for Identifiers in the Caché Programming Orientation Guide. For further details on using globals as subscript arrays, see Using Caché Globals.
Variable Typing
Variables in Caché Basic are, like in ObjectScript, JavaScript and VBScript, untyped. This means that you can assign a string value to a variable and, later on, a numeric value to the same variable. Caché automatically converts the value of variables to other types, based on the context in which it is used.
Conversion amongst the various types are carried out using the same rules as ObjectScript.
Caché Basic Type Conversion Rules
From To Rules
Number Object Not allowed.
Number String The number is converted to a string of numeric characters that corresponds to the number's value: 22 becomes “22”.
Object Number The object reference is converted to an integer whose value is the object instance number.
Object String The object reference is converted to a string in the form: “oref@classname” where oref is the object instance number and classname is the type of object.
String Number The string is parsed left-to-right until a non-numeric character is encountered. The leading numeric characters are converted to the corresponding numeric value. If there are no leading numeric characters, the string is converted to 0. For example, “123” converts to 123, “123abc” converts to 123, and “abc123” converts to 0.
String Object Not allowed.
Variable Declaration
Declaring variables in Caché Basic is not required. If you want to take advantage of detecting undeclared variables by the compiler you can use the Option Explicit clause at the beginning of your program. In that case you must declare all variables using the Dim statement:
Option Explicit
Dim a
Variable Scope
Variables used in Caché Basic are visible within the routine, unless a procedure or function explicitly declares the variable using the Dim statement.
Default Variable Values
When initially created, a variable is Empty, which indicates that no value has been assigned to the variable. Empty variables are 0 in a numeric context, or zero-length in a string context. Unlike ObjectScript, invoking an undefined variable does not result in an error. If no value has been assigned to a variable, invoking that variable returns the empty (null) string.
Maximum Number of Variables
A Caché Basic routine can contain a maximum of 32,759 private variables, and a maximum of 65,280 public variables. Exceeding these limits results in a compile error.