Skip to main content

Variables

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.

  • Local variable names must begin with an alphabetic or percent character (%). The second and subsequent characters can be alphabetic characters, numbers, or the underscore (_) character. The scope of local variables is limited to the current process. Local variables are accessible from any namespace. The following are all valid local variable names:

    a = 10
    A = 20
    a(1,3) = "subscripted"
    %A = "percent and letter"
    %5 = "percent and number"
    aa = "letters"
    a5 = "letter and number"
    aBcDeFgHiJkLmNoPqRsTuVwXyZ = "alphabet"
    a_e_i_o_u_ = "vowels"
    PrintLn "a = " & a
    PrintLn "A = " & A
    PrintLn "a(1,3) = " & a(1,3)
    PrintLn "%A = " & %A
    PrintLn "%5 = " & %5
    PrintLn "aa = " & aa
    PrintLn "a5 = " & a5
    PrintLn "aBc... = " & aBcDeFgHiJkLmNoPqRsTuVwXyZ
    PrintLn "a_e_i_o_u_ = " & a_e_i_o_u_

    Note that local variable names can include underscore (_) characters, but cannot include dot (.) characters.

  • Process-private globals in Caché Basic can take either of the following syntactical forms: ^||name or ^|"^"|name. Other process-private global syntactical forms are not supported in Caché Basic. The first character of a process-private global name can be either an alphabetic or percent character (%). The second and subsequent characters can be alphabetic characters, numbers, or dot (.) characters. A dot character (period) may not be the first or last character of the name. The scope of process-private globals is limited to the current process. Process-private globals are accessible from any namespace. The following are all valid process-private global names:

    ^||a = 10
    ^|"^"|A = 20
    ^||a(1,3) = "subscripted"
    ^||%A = "percent and letter"
    ^||%5 = "percent and number"
    ^||aa = "letters"
    ^||a5 = "letter and number"
    ^||aBcDeFgHiJkLmNoPqRsTuVwXyZ = "alphabet"
    ^||a.e.i.o.u = "vowels"
    PrintLn "^||a = " & ^||a
    PrintLn "^|"^"|A = " & ^|"^"|A
    PrintLn "^||a(1,3) = " & ^||a(1,3)
    PrintLn "^||%A = " & ^||%A
    PrintLn "^||%5 = " & ^||%5
    PrintLn "^||aa = " & ^||aa
    PrintLn "^||a5 = " & ^||a5
    PrintLn "^||aBc... = " & ^||aBcDeFgHiJkLmNoPqRsTuVwXyZ
    PrintLn "a.e.i.o.u = " & ^||a.e.i.o.u

    Note that process-private global names can include dot (.) characters, but cannot include underscore (_) characters.

  • Globals in Caché Basic can take any of the following syntactical forms: ^name, ^|""|name, or ^|"namespace"|name. Global syntactical forms using square brackets are not supported in Caché Basic. The first character of a global name can be either an alphabetic or percent character (%). The second and subsequent characters can be alphabetic characters, numbers, or dot (.) characters. A dot character (period) may not be the first or last character of the name. The scope of globals is not limited to the process that created it. A global is stored in the database and is therefore visible to all processes. A global persists after the process that created it terminates. A global is specific to a namespace, and can only be access from that namespace, or by referencing that namespace. The following are all valid global names:

    ^a = 10
    ^|"samples"|A = 20
    ^a(1,3) = "subscripted"
    ^%A = "percent and letter"
    ^%5 = "percent and number"
    ^|""|aa = "letters"
    ^a5 = "letter and number"
    ^aBcDeFgHiJkLmNoPqRsTuVwXyZ = "alphabet"
    ^a.e.i.o.u = "vowels"
    PrintLn "^a = " & ^a
    PrintLn "^A = " & ^|"samples"|A
    PrintLn "^a(1,3) = " & ^a(1,3)
    PrintLn "^%A = " & ^%A
    PrintLn "^%5 = " & ^%5
    PrintLn "^aa = " & ^|""|aa
    PrintLn "^a5 = " & ^a5
    PrintLn "^aBc... = " & ^aBcDeFgHiJkLmNoPqRsTuVwXyZ
    PrintLn "a.e.i.o.u = " & ^a.e.i.o.u

    Note that global names can include dot (.) characters, but cannot include underscore (_) characters.

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.

FeedbackOpens in a new tab