Top  >  Lycia reference  >  Querix 4GL  >  Statements  >  CONSTANT

CONSTANT

 

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.

 

Usage

 

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.

 

Private and Public Constants

 

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.

 

Usage Restrictions

 

There are several restrictions as to in which statements and situations constants cannot be used:

 

 

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.

      

CONSTANT a = 123 

LET a = 567 -- this line will cause an error