Enumerations
An enumeration is a list of named constants. It begins with the DCL-ENUM statement, followed by zero or more constant definition statements, followed by the END-ENUM statement.
DCL-ENUM and END-ENUM are only available as free-form statements, but the constant definitions can be either free-form or fixed-form.
A data type can be specified for an enumeration, allowing you to define variables like the enumeration. For more information on typed enumerations, see Typed Enumerations.
The DCL-ENUM statement
DCL-ENUM myEnum;
DCL-ENUM myEnum QUALIFIED;
c1 100;
c2 200;
END-ENUM;
DSPLY myEnum.c1;
The END-ENUM statement
END-ENUM is optionally followed by the name of the enumeration. If the name is specified, it must be the same as the name specified for the DCL-ENUM statemnet.
Rules for enumeration constants
Additional rules apply to typed enumerations. See Typed Enumerations.
- Figurative constants are not allowed as the value for an enumeration constant.
- %ADDR and %PADDR are not allowed as the value for an enumeration constant.
- All the constants for an enumeration must have the same type except that hexadecimal literals can be in the same enumeration as character literals or numeric literals.
- The constants for an enumeration can be specified in free-form or fixed-form.
The
DCL-Coperation code can be used if the name is the same as an operation code used in free-form calculations. In the following example,LEAVEis an operation code allowed in free-form calculations, soDCL-Cmust be used to define the constant.DCL-ENUM what QUALIFIED; handle 'HDL'; DCL-C leave 'LV'; keep 'KP'; END-ENUM; - Usually, the enumeration constants are specified with free-form statements, but it may be convenient to
specify them in fixed-form statements if they are already in a /COPY file.
For the following example, assume member ENDLINE of file ENDLINE contains the following fixed-form statements:
D NEWLINE C X'15' D LINEFEED C X'25' D CARRIAGE_RETURN... D C X'0D'The enumeration constants are defined by the /COPY statement:DCL-ENUM endLineChars QUALIFIED; /COPY QRPGLESRC,ENDLINE END-ENUM; print_line = line + endLineChars.NEWLINE;
Using an enumeration constant
DCL-ENUM name_lengths QUALIFIED;
system_object 10;
ifs 5000;
END-ENUM;
DCL-S library CHAR(name_lengths.system_object);
DCL-S path VARCHAR(name_lengths.ifs);
DCL-S name VARCHAR(20);
IF %LEN(name) <= name_lengths.system_object;
...
ENDIF;
Using an enumeration
- SORTA
- %ELEM
- %LOOKUP
- %SUBARR
- It is assigned to an array.
- It is used in a FOR-EACH statement to work with each constant in the enumeration.
- It is used with the IN operator to test whether the value of colors is one of the constant values in the enumeration. In this example, the value of colors is not one of the values in the enumeration, so the IF statement is false.
DCL-ENUM colors QUALIFIED;
green '0,255,0';
red '255,0,0';
blue '0,0,255';
END-ENUM;
DCL-S color CHAR(10);
DCL-S arr VARCHAR(20) DIM(3);
arr = colors; // 1
// arr(1) = "0,255,0"
// arr(2) = "255,0,0"
// arr(3) = "0,0,255"
FOR-EACH color IN colors; // 2
DSPLY color;
ENDFOR;
// It displays
// "0,255,0"
// "255,0,0"
// "0,0,255"
color = '255,255,255';
IF color IN colors; // 3
...
For more examples, see Examples of literals used in enumerations.
