Previous Topic Next topic Print topic


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 public.
working-storage section. 
01 binary-long.         *> Underlying type for the enum. 
                        *> binary-long is the default
    78 Starter value 0. *> Declare enum values as level-78 items
    78 Stop    value 1.
    78 Rewind  value 2.
    78 Forward value 3.
end enum. 

See also the Enums sample, available from $COBDIR/demo.

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 .NET, but not JVM, you can set the attribute System.FlagsAttribute. This attribute applied to an enum means that the distinct values are to be regarded as bit settings, and can therefore be combined. For example:

enum-id MyFlags attribute System.FlagsAttribute.
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.

Previous Topic Next topic Print topic