It is now possible to define logical sub-blocks in 4GL code using the BEGIN .. END syntax:
Element |
Description |
Statement List |
One or more executable statements, either 4GL or SQL |
Declaration Clause |
It is an optional DEFINE statement with the list of the declared variables |
A logical sub-block may contain its own variable declarations, whose scope is restricted to the sub-block. Such sub-blocks can occur in the MAIN or FUNCTION program blocks as well as within complex statements which have clauses with executable statements: CASE, IF, FOR, FOREACH, INPUT, INPUT ARRAY, DISPLAY ARRAY, etc.
For example, we can insert a sub-block in the INPUT statement:
INPUT BY NAME customer_rec.*
AFTER FIELD fname
BEGIN
DEFINE i INTEGER
DEFINE val LIKE customer.fname
#variables ‘i’ and ‘val’ exist only within the scope of the current #BEGIN…END block
LET val = get_fldbuf(fname)
SELECT COUNT(*) INTO i FROM customer WHERE customer.fname = val
IF i <> 0 THEN
ERROR "Must enter a unique name"
END IF
END
…
END INPUT
Forms opened with the help of the OPEN FORM statement within such sub-block cannot be displayed with the help of the DISPLAY FORM statement outside the sub-block, as their scope of reference is the sub-block only. This is true for other objects such as cursors, reports, etc. To be able to use objects outside the BEGIN…END block, you should declare these objects with the GLOBAL scoping. Objects declared as global acquire global scope of reference which includes all the program modules. Objects open without scoping specification or with the LOCAL scoping cannot be references outside the logical sub-block.