Enums

An enum (short for enumeration) represents a list of constant values, such as values for the days of the week. You can declare an enum type that defines the values and symbolic names for them. You can then refer to the values by name.

enum-specification

Specification for enums
 enum-header type-specifier enum-field

enum-header

Syntax for the enum header
 access-modifier attribute-clause

enum-field

Syntax for enum fields type-specifier type-specifier

Example

enum-id Action.
   78 #Start.   *> Start is a reserved word so use '#' symbol
   78 #Stop.
   78 #Rewind.
   78 #Forward.
end enum.

enum-id Status.
   78 Flunk value 50.
   78 Pass  value 70.
   78 Excel value 90.
end enum.

class-id MainClass.
method-id main static.
   declare a = type Action::Stop
   if a not = type Action::Start
       display a & " is " & a as binary-long   *> Prints "Stop is 1"
    end-if
   display type Status::Pass as binary-long    *> prints 70
   display type Status::Pass *> prints "Pass"
end method.
end class.

An example with 01 level entries:

       enum-id Permission.
       01 #Execute value 1 b-left 0.
       01 #Write value 1 b-left 1.
       01 #Read value 1 b-left 2.
       end enum.

See also the Enums sample, available from Start > All Programs > Micro Focus Visual COBOL > Samples , under Language Reference for JVM COBOL (Windows) or $COBDIR/demo (UNIX).

Further Information

The enum-header is followed by one or more field definitions which obey the following rules:

  • The default underlying type of an enum is binary-long (such as a signed 32-bit integer).
  • If the first enum field is a 01 level entry, then it may explicitly define the underlying type of the enum, by means of the type-specifier. Additionally, if it has a name, it is also the first constant field of the enum.
  • Subsequent constant fields may be defined using either level 78 entries, or level 01 entries. For 01 level entries, the word CONSTANT is assumed and is therefore optional.
  • If the first constant field does not have an explicit VALUE clause, it is assumed to have a value of 0.
  • All subsequent constant entries will either have an explicit value, or they will assume a default value which is 1 greater than the value of the previous entry.
  • The constant expression associated with a level 78 entry is evaluated according to the normal rules for level 78 entries, i.e. arithmetic operations are evaluated from left to right, without regard to operator precedence.
  • The constant expression associated with a level 01 entry is evaluated according to the normal rules for level 01 constant entries. VALUE expressions in these constant entries may reference external constants, and any arithmetic operations which they define are evaluated according to the normal arithmetic precedence rules.
Note:
  • In JVM COBOL, as in Java, the enum declaration defines an enum class, which is a class that derives from java.lang.Enum. In Java, this enum class can include methods and other fields explicitly declared by the user, whereas this is not allowed in COBOL.
  • In JVM COBOL, the Compiler adds a static method called 'values' that returns an array containing the values of the enum. You can use this method together with the PERFORM THROUGH construct to iterate over the values of an enum type. For example:
           enum-id E1.
           78 #Zero.
           78 One.
           end enum.
           program-id x.
               display type E1::Zero
               display type E1::One
               perform varying auto e through type E1::values
                   display e
               end-perform