Variables & Data Types
There are five (5) data types in Capital Business Script (CBS) that may be assigned to variables. These are:
Numeric
Character/String & Transaction
Logical
Date
Array
A variable, once declared, can be assigned a value of any data type whereupon it takes on that type.
Numeric
A numeric variable can hold any number providing it has no more than 19 digits. It can have 18 decimal places and is always signed. (Can hold positive and negative values.)
Character/String
These variables can hold any of the 256 characters in the ANSI or ASCII character set. The size of a character variable is determined by the length of the string it contains and can be changed simply by changing the contents of the string. (Character variables are also commonly referred to as strings.) The maximum size of any single character variable is 64K.
Transaction
This is essentially a variation on character/string variables. Transaction type variables normally contain only letter and number symbols. They are treated by CAPITAL as character strings. Almost all transaction variables in CAPITAL are 9 digits long. They cannot contain blank spaces, so the transaction number 1000 would be stored in a transaction variable as 000001000.
Date
As the name suggests these variables hold date values. In CBS it its easy to perform a comprehensive range of date based calculations. Date variables may be assigned 2 or 4 digit years. If a two digit year is assigned, then the digits from 0 to 50 are considered as belonging to the 21st century. The digits between 51 and 99 are assigned to the 20th century.
Logical
These variables can only hold the values true or false. The CBS constants for these are TRUE and FALSE respectively.
Array
This is both a variable and a complex data structure. Each element of an array can be of any variable type including other arrays. The maximum number of elements in any one dimension is 4096. This does not prevent you from having 4096 elements, each of which is another array. There is a theoretical maximum number of elements for any single array containing sub-arrays but this limit is very large.
Declaring Variables
Before they can be assigned values, variables must be declared. The format of the declaration statement is:
DECLARE [GLOBAL] x TYPE Variable Type [Format Picture]
GLOBAL - The statement GLOBAL is optional. Define variables as GLOBAL when you want to retain them even when different forms are loaded. Variables are normally scoped to the form in which they are declared and are not visible to other forms. Declaring variables as global can permit you to prompt users for a series of questions once only, then run multiple forms (reports) together. If you want to erase a normal or globally declared variable this can be done using the RELEASE command.
x - The variable name. Only the first 10 characters are significant. Variables must start with a letter of the alphabet and must consist of a combination of letters and/or numbers and the underscore "_" character. Other characters are not valid. It is important that variable names do not clash with field names from any databases being processed. This will avoid confusion over whether the data being accessed is from a variable or a database field.
TYPE Variable Type - Refers to the variable type as discussed above. This may be either:
DATE
CHARACTER
LOGICAL
NUMERIC or
ARRAY
(You can use the terms STRING or CHAR instead of CHARACTER if you prefer.)
[Format Picture] - A picture format is optional. It is useful to define a picture format if a variable will be used to hold a response from a user question.
A picture format is also sometimes referred to as a template. They include:
|
Template Symbol |
Description |
|
A |
Allow only alphabetic characters |
|
N |
Allow only alphabetic and numeric characters |
|
X |
Allow any character |
|
9 |
Allow digits for any data type including sign (the minus or dash) for numerics |
|
# |
Allow digits, signs and spaces for any data type |
|
L |
Allow only T, F, Y or N |
|
Y |
Allow only Y or N |
|
! |
Convert alphabetic character to upper case |
|
. |
Display a decimal point |
|
, |
Display a comma |
The following CBS statement declares an empty character string:
Declare MyVar Type String
The statement below declares the same string but limits it to 4 characters. Each character must be numeric. (This would be useful for limiting user input when prompting for, say, a postcode.)
Declare MyVar Type String Picture 9999
Note how the picture format can be used to limit the size of a character string. For example, to accept only 10 characters:
Declare MyVar Type String Picture XXXXXXXXXX
The following statement declares a variable as a date. Note that for dates no special picture formatting is required:
Declare MyVar Type Date
The following statement declares a numeric variable:
Declare MyVar Type Numeric
At times CAPITAL may automatically declare variables for you. If you see a Result In field on a dialog, you should only enter the variable name. CAPITAL will automatically declare/create the variable. Other variables should be declared as shown above. Automatically declared variables can be used just like variables that have been declared normally.
Once a variable has been declared, it may be reassigned another value. Although CBS will not prevent you, it is recommended that you do not change the variable's type. A character variable should always hold only characters, numeric variables only numbers. Failing to obey this guideline will make your CBS scripts difficult to "debug".
A variable is only declared by CAPITAL once. If it already exists, any attempt to declare it again is ignored. You can use the RELEASE command to remove the variable if you wish to redeclare it. For example:
RELEASE MyVar