Top > Lycia reference > Querix 4GL > Statements > CONSTANT
To meet your needs, we constantly work to improve Querix products.
This means that Lycia documentation is developing as well.
In case you have found a certain dissonance between the provided information and the actual behavior of Lycia 3 and/or your applications, please, let us know about this via documentation@querix.com so that we can introduce the necessary changes to our documentation.
Thank you for your attention and cooperation.
CONSTANT defines a constant which, like a variable has a name, but unlike a variable also has a predefined and unchangeable value. Constants can be useful for declaring special symbols and symbol combinations such as "\n".
The CONSTANT statement has the following syntax:
Element |
Description |
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. |
Datatype |
The optional data type specification of the constant |
Value |
The unchangeable value of the constant |
Just like variables, constants can have global, module and local scope of reference depending on the position where they are declared. The constants can be used throughout the 4GL code within their scope of reference after they were declared. Constants can be passed to functions and returned by them.
The data type definition for a constant is optional. If it is not specified, the literal value assigned to the constant defines its data type implicitly. For example:
CONSTANT const1 = 5 -- the data type is defined automatically as INT
CONSTANT const2 = "yes" -- the data type is STRING
CONSTANT const3 CHAR(3) = "5" -- the data type is CHAR(3)
If you explicitly specify one of the character data types for a constant, make sure that the constant value is enclosed in the quotation marks. Since the implicit data type conversion does not occur with the constants, assigning a number to a constant without enclosing it in quotes will cause an error, whereas assigning it to a character variable will just result in automatic conversion.
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.
Constants can be used in the consequent DEFINE statements, e.g.:
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.
The constants defined at the beginning of a module have module scope of reference by default. They are visible 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.
To declare a public constant, specify the PUBLIC keyword before the CONSTANT statement. A public constant can be referenced by other modules of the same program. It can also be referenced, if the module is imported using the IMPORT statement.
There are several restrictions as to in which statements and situations constants cannot be used:
It 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 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 in any way. The following line will throw a compile time error:
CONSTANT a = 123
LET a = 567 -- this line will cause an error