The CONSTANT instruction defines a constant that has a name and a predefined, unchangeable value (unlike a variable value that can be changed). The constants can be used throughout the 4GL code within their scope of reference after they were declared.
Constants can be useful in declaring special symbols and symbol combinations such as "\n".
|
Identifier |
The name of the constant. It should be unique and differ from the names of the variables available in the same scope of reference. |
|
Data Type |
A Simple or a Structured data type (see the full list in the Specifying the Data Type section of this page ↓). |
|
Value |
The unchangeable value of the constant. Cannot be NULL. |
Example:
CONSTANT const CHAR(3) = "5" -- the data type is CHAR(3)
If no scope has been specified, the constants are private (have a Module scope). Constants can have a global, a module, or a local scope of reference depending on the position where they are declared.
The constants defined at the beginning of a module have the Module scope of reference by default. They are visible to and can be used only in the current module. You can declare a private constant explicitly by specifying the PRIVATE keyword before the CONSTANT statement.
A public constant can be referenced by other modules of the same program. To make a constant public, use the PUBLIC keyword before the CONSTANT keyword in the constant declaration. After that, the public constant can also be referenced using the IMPORT statement.
The data types you can declare as a CONSTANT type are the following:
| INTEGER | SMALLINT | BIGINT |
| BOOLEAN | MONEY | |
| DECIMAL | FLOAT | SMALLFLOAT |
| CHAR | VARCHAR | |
| DATE | DATETIME | INTERVAL |
| RECORD | ARRAY | DYNAMIC ARRAY |
Example:
TYPE MyArrayType ARRAY[3] OF INT
TYPE MyDynamicArrayType DYNAMIC ARRAY OF INT
CONSTANT global_r3 MyArrayType = [ 11343, "1223", "3"]
CONSTANT global_r4 MyDynamicArrayType = [ 194, "164", 14454.4,"4" ]
CONSTANT r1 RECORD v1 INT, v2 STRING END RECORD = (v1: 5, v2: "11A")
If the data type has not been specified, the literal value assigned to the constant can define its data type implicitly.
Example:
CONSTANT const1 = 5 -- the data type is defined automatically as INT
CONSTANT const2 = "yes" -- the data type is STRING
The CONSTANT declarations are processed / compiled prior to the normal DEFINE variable declarations regardless of where they are located within the code. For example:
MAIN DEFINE b CHAR(a) #using the constant a CONSTANT a INT = 123 #declaring the constant a ... MAIN CONSTANT a INT = 123 #declaring the constant a DEFINE b CHAR(a) #using the constant a ...
If the data type is declared and it has the scale and / or precision, make sure that the literal value assigned corresponds to the precision. For example, if you assign value "abcde" to a constant with the data type declared as CHAR(3), the content of the constant will be truncated:
CONSTANT t CHAR(3) = "abcde"
DISPLAY t -- This will display abc
CONSTANT i = 10
DEFINE ar ARRAY[i] OF INT
With other 4GL statements constants are usually utilized in places where the literals would normally be placed:
CONSTANT n = 100
...
FOR i = 1 TO n
Naturally, if a constant was not defined using the CONSTANT statement and then used in the code, a Compile Time error will be thrown.
There are several restrictions as to in which statements and situations constants cannot be used:
A constant cannot be used in the ORDER BY clause of the SELECT statement;
It cannot be used in cases where the automatic conversion of data types takes place: constants are not converted automatically. Thus, if you have a constant of a character data type with the value 123, you cannot use it in an arithmetic expression:
CONSTANT a CHAR(3) = 123 DISPLAY 100+a -- you will get 100 as a result
For the same reason the constant above cannot be used in other cases where an integer value is expected, e.g. as the size of an array during the array declaration, as a counter index in the FOR statement, and so on;
Values cannot be assigned to a constant after its value has been declared. The following line will throw a Compile Time error:
CONSTANT a = 123 LET a = 567 -- this line will cause an error
The compiler will throw a Compile Time error if you try to define the CHAR precision with a symbol not defined with an integer:
CONSTANT a CHAR(3) = "123" DEFINE b CHAR (a) -- this line will cause an error