Free-Form Enumeration Definition

An enumeration 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.

The DCL-ENUM statement

DCL-ENUM is followed by the name of the enumeration.

DCL-ENUM myEnum;
The QUALIFIED keyword can be specified to indicate that the constant names must be qualified by the enumeration name when they are used.

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

  • 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-C operation code can be used if the name is the same as an operation code used in free-form calculations. In the following example, LEAVE is an operation code allowed in free-form calculations, so DCL-C must 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

You can use enumeration constants anywhere a named constant can be used.

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

You can use the name of the enumeration in calculation expressions wherever an array can be used except:
  • SORTA
  • %ELEM
  • %LOOKUP
  • %SUBARR
In the following example, the colors enumeration is used in several places that an array can be used.
  1. It is assigned to an array.
  2. It is used in a FOR-EACH statement to work with each constant in the enumeration.
  3. 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.