Constants

Constant is a code element, which value is set once and remains constant during program execution. Constants can be determined in the global namespace, in proper namespaces, classes, procedures, functions, and properties.

Namespace constants are available in namespaces and in all child elements of the code. Constants declared in classes can have the access modifier that determines their visibility scope. Constants declared in procedures/functions/properties can be used only within these objects.

All constants must be described in the separate section, which starts with the Const reserved work. A constant type is not specified but is determined automatically during constant value analysis. If a constant is determined in a class, the Const keyword is specified for each constant.

It is possible to determine a constant by expression calculation based on previously determined constants. All mathematical operators (+, -, /, *, div, mod), logical operators (not, or, and, xor), and relation operators may be used in expressions.

Const
    <identifier1> = <value1>;
    <identifier2> = <value2>;

Class <class>: Object
    Const <identifier1> = <value1>;
    Const <identifier2> = <value2>;
    
End Class <class>;

Reserved Constants

Fore has reserved constants with fixed vales. In the code these constants are highlighted as keywords.

Name Constant description
True It returns logical True.
False It returns logical False.
Null It provides an empty reference that does not refer to any object. Setting it as a value causes resetting of the value and releasing resources spent to store value. Null as a value can be set for variables of one of the following types of data:
  • System or custom object.
  • Interface.
  • Variant;
  • Array.
On an attempt to specify Null as the variable value of a simple data type, the exception is thrown.

Example

Const
    _globlaVersion = 1.0;
    _globlaName = "Programm";

Namespace TestNamespace
Const
    _FileName = "Output";
    _Path = "c:\Work\";
    _Max = 255;
    _Min = 0;

    Public Class Test: Object
        Public Const _clsConst1 = 1;
        Const _clsConst2 = "#FF0000";

        Public Property FilePath(Index: Integer): String
            Get
            Const
                _ext0 = "xml";
                _ext1 = "txt";
                _ext2 = "dat";
            Begin
                If Index = 0 Then
                    Return _Path + _FileName + '.' + _ext0;
                Elseif Index = 1 Then
                    Return _Path + _FileName + '.' + _ext1;
                Else
                    Return _Path + _FileName + '.' + _ext2;
                End If;
            End Get
        End Property FilePath;

        Public Function GetColor(_Default: Boolean = True): String;
        Const
            _funcConst = "#FFFF00";
        Begin
            If _Default Then
                Return _clsConst2;
            Else
                Return _funcConst;
            End If;
        End Function GetColor;
    End Class Test;
End Namespace TestNamespace;

See also:

Fore Language Guide | Variables