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 COBOL for .NET .

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 COBOL for .NET, 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.