Language Elements and Reference:xpplrm

Variables Foundation

A variable is used in a program to store or retrieve a value. Variables are represented within program code by identifiers. Within the program code the value (or contents) of a variable is accessed using the identifier. The identifier is a place holder for the contents of a variable.

Xbase++ distinguishes between two variable types: memory variables and field variables. The values of memory variables are held in memory during runtime of a program. The values of field variables are saved in a file. Values of field variables continue to exist after termination of the program.

In Xbase++, memory variables are not "strongly typed". This means a value of any size and any data type can be assigned to any variable (see the chapter "Data Types and Literals"). A memory variable containing a value of character data type can later be assigned a value with another data type. Memory variables differ from field variables in this aspect.

As a rule, field variables are "strongly typed" and can generally only store values of a specific data type. The values of field variables are stored in a file managed by a DatabaseEngine (see the chapter "The Xbase++ DatabaseEngine"). The data type and size of values that can be stored in a field variable depend on the file format and on the DatabaseEngine managing the file.

Whether Field variables are "strongly typed" is determined by the underlying file format accessed by the DatabaseEngine.

Variables can be further classified into the categories lexical variables and dynamic variables. These categories also determine how references to variables are resolved and how program code is executed for the variable. Lexical variables must always be declared. The compiler can then resolve references to each memory variable. Dynamic variables can be declared, but it is not required because references to dynamic variables are resolved during runtime of the program. Lexical variables are more efficient than dynamic variables at runtime. Access is more rapid since the compiler can generate optimized code with lexical variables.

Memory variables can be either lexical or dynamic, while field variables are always treated as dynamic variables.

Field variable Memory variable
Dynamic Yes Yes
Lexical No Yes
Strongly typed Yes No
Declaration Can Can(dynamic)/Must(lexical)
DatabaseEngine Yes No

Naming field variables

There is no convention for naming field variables except that the variable name should describe the contents of the field variable, if possible. For a file containing data for addresses, "LASTNAME", "FIRSTNAME", "STREET" or "CITY" could be meaningful identifiers for naming various field variables.

Naming memory variables

A modified Hungarian notation has proved an effective convention for naming memory variables. Variable names following this convention begin with a prefix in lower case followed by the actual variable name in mixed case. The prefix identifies the data type (see the chapter "Data Types and Literals") and the rest of the variable name identifies the contents of the variable. Examples of Hungarian notation are:

aPos    = {10,20}     // (a) Array, (Pos) Row+Column position 
bCompare= {|| x<y }   // (b) Code block, (Compare) 
cString = "Character" // (c) Character value, (String) 
dToday  = Date()      // (d) Date value, (Today) 
lOk     = .F.         // (l) logical value, (Ok) 
nRow    = 10          // (n) numeric value, (Row) Row position 
oGet    = GetNew()    // (o) Object, (Get) Get object 
xValue  = MyFunc()    // (x) undefined value, (Value) a value 

The prefix specifies the type of value contained in the variable. This can greatly reduce the amount of comments in a program. Using this technique, the identifier of a variable includes information on the data type as well as the purpose of the contents of the variable within the program code.

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.