Constants

A constant is a value that remains unchanged during the course of code execution.

In Uniface, it is possible to use the following types of constant as operands in expressions:

Compile-Time Constants

It is possible to define named constants which are compiled into your code during compilation. Known as compile-time constants or precompiler constants, they can be used to improve the readability of your code and make your application easier to maintain.

They make it possible to give cryptic status values meaningful names, or centralize definitions that may change from time to time, in a single location.

Compile-time constants are declared using the pre-compiler directives #define or #startdefine/#enddefine (for blocks of text).

Defining and Referencing Compile-Time Constants

To declare a compile-time constant, use the #define pre-compiler directive. For example:

#define APP_VERSION  = "2.1.00"

To reference the constant in ProcScript, enclose the name within angle brackets as <ConstantName>. For example:

putmess <APP_VERSION>

During compilation, Uniface replaces the constant name with the actual value.

It is also possible to remove a constant using the #undefine directive. #define and #undefine

Predefined Uniface Constants

Uniface provides a set of predefined constants that can be used in ProcScript to refer to objects and properties, such as a field or entity. For example: <$componentName> and <$entName>. For more information, see ProcScript: Precompiler Directives and Uniface Constants.

Note:  These constants should only be used in components, not in global ProcScript, because at compile time, global ProcScript does not have access to the information required to correctly resolve the compile-time constants.

Implicit Constants in Derived Components

In components that are derived from modeled components, Uniface generates implicit constants for generic entities and fields based on the Bound To property. For more information, see Generic and Bound Entities and Fields.

Scope of Constants

Named constants can be declared anywhere, but the location where they are declared affects their availability or scope.

Note:  A constant always loses scope when it is explicitly removed using the #undefine directive.

Scope of Constants
Scope Where Defined Description
Block scope Inside a constant scope block delimited by #startdefine and #enddefine Available to all script inside the constant scope block. For more information, see #startdefine.
Object scope Declarations code container of a component, entity, or field Available for the entire object and its immediate child objects.

Note:  Constants defined in the Declarations container of a component entity are visible to its fields, but not to its inner entities.

Container scope Script code container Available to all code in the container—all triggers, all operations, and all entries.
Global scope IncludeScript called DEFPARAM Known as a global constant, it is available to all ProcScript in the application as long as it has access to the IncludeScript library. Each library can contain one DEFPARAM IncludeScript.

Global constants defined in the SYSTEM_LIBRARY are available to all your Uniface applications.

For more information, see Define a Global Constant.

Include scope IncludeScript When defined in any other IncludeScript, it is available within the code where it is inserted by the #include directive.

Tip: If you define constants in an IncludeScript, it is good practice to undefine them at the end of the IncludeScript.

When a constant is defined twice, the compiler generates an information message. The constant defined at the lowest level is used.

Search Order

When the pre-compiler encounters a reference to a constant, it looks for the constant definition in the following order:

  1. In the #startdefine/#enddefine block where it is referenced
  2. In the Script container where it is referenced
  3. In the Declarations container of the object where it is referenced, for example the Declarations container of a field
  4. In the Declarations container of an object that the object inherits from, for example the Declarations container of a modeled field
  5. In the Declarations container of a parent object, for example the Declarations container of the entity, if the object is a field
  6. In the Declarations container of the component
  7. In the Declarations container of the modeled component
  8. In the DEFPARAM IncludeScript in the component library
  9. In the DEFPARAM IncludeScript in SYSTEM_LIBRARY

Related Topics