Enums

An 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

enum-header type-specifier enum-constant

enum-header

access-modifier attribute-clause

enum-constant

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 
end method.
end class.

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

Further Information

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.

The enum-header is followed by a working-storage section consisting of:

  • A single numeric field of one of the predefined types. By default, this is binary-long.
  • Any number of level 78 items defining the values for the enum. The default value of the first item is 0 and the values of subsequent items are incremented by default.
Note: In COBOL for JVM, 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.