DEFINE
The DEFINE
instruction declares a program variable with a given
type.
Syntax
[
PUBLIC|
PRIVATE]
DEFINE
{
identifier [
,...]
type-specification
|
identifier type-specification [
= initializer ]
}
[
,...]
- identifier is the name of the variable, that must follow the convention for identifiers.
- type-specification can be one of:
- A primitive type
- A record definition
- An array definition
- A dictionary definition
- A function type definition
- The name of a user defined type
- The name of a built-in class
- The name of an imported extension class
- The name of an imported Java class
- initializer is a variable initializer, that corresponds to the type-specification. For example, if the type-specification is a record definition, the initializer must be a record initializer.
Usage
A variable is a named location in memory that can store a single value, or an ordered set of values. Variables can be global to the program, module-specific, or local to a function.
Any program variable needs to be declared with a DEFINE
statement before
it is used.
By default, module-specific variables are private; They cannot be used by an other module of
the program. In order to improve code re-usability by data encapsulation, we recommend you keep
module variables private, except if you want to share large data (like arrays) between modules.
To make a module variable public, add the PUBLIC
keyword before
DEFINE
. When a module variable is declared as public, it can be referenced in
another module by using the IMPORT FGL
instruction.
When defining variables with the LIKE
clause, the data types are taken from the
database schema file at compile time. Make sure that the schema file of the database schema during
development corresponds to the database schema of the production database; otherwise the variables
defined in the compiled version of your modules will not match the table structures of the
production database. For more details, see Database column types.
To write well-structured programs, avoid global variables. If you need persistent data storage
during a program's execution, use variables local to the module and give access to them with
functions, or make the module variables PUBLIC
to other modules. For more details,
see Declaration context.
Variables can be defined with the ATTRIBUTES()
clause, to specify
meta-data information for the variable. For more details, see Variable attributes.
Variables can be defined to hold a function reference. Best practice is to declare a user-defined type with the function type, then define the variables using the user type.
By adding an equal sign followed by a value, you can save an additional LET
instruction to initialize variables.
Initializers are especially useful to set values of structured variables (records, arrays) . For
more details, see Variable default values.