Modern ABAP

Enumerations - Enumeration types

“(C) Brandeis Consulting”

Examples of constant values in programs

CONSTANTS:
gc_green TYPE gty_light_state VALUE 0,
gc_yellow TYPE gty_light_state VALUE 1,
gc_red TYPE gty_light_state VALUE 2,
gc_red_and_yellow TYPE gty_light_state VALUE 3.
-----
IF sign = 'EQ' or sign = 'NE'.
...

“(C) Brandeis Consulting”

Concept of enumerations

When defining enumerations (German: enumeration objects), two things are created at the same time:

  • A data type
  • One or more constant values with this data type
TYPES: BEGIN OF ENUM ecolour,
colour_red,
colour_green,
colour_blue,
colour_yellow,
colour_orange,
colour_violet,
END OF ENUM ecolour.

At design time, the system checks that all assignments and comparisons always use exactly the same type. This means that it is not possible to use other values.

“(C) Brandeis Consulting”

Enumerations - Simple example

TYPES: BEGIN OF ENUM ecolour,
colour_red,
colour_green,
colour_blue,
colour_yellow,
colour_orange,
colour_violet,
END OF ENUM ecolour.

METHODS get_colour_text
IMPORTING colour TYPE ecolour
RETURNING VALUE(result) TYPE STRING.

....
CLASS xyz IMPLEMENTATION.

METHOD get_color_text.
IF color = color_red.
result = |Beautiful color|.
ENDIF.
ENDMETHOD.

All users refer to exactly this one definition:

  • The type ECOLOUR can be used to define variables or parameters.
  • The values COLOUR_RED etc. can be used as constants.
“(C) Brandeis Consulting”

Advantages of using enumerations

  • Only permitted (i.e., defined) values can be used. This means that program logic can rely on them 100%.
  • There is exactly one central definition. This helps, for example, when searching for uses of the values.
  • Type checking does not allow assignments from other data types or enumerations, even if the same data type is used internally. This prevents the same constant from being defined multiple times.
  • The internal data type is largely irrelevant.
“(C) Brandeis Consulting”

Constants in structures - STRUCTURE

The defined constants can also be defined as components of a structure. This allows them to be neatly grouped.

TYPES: BEGIN OF ENUM ecolour STRUCTURE color,
red,
green,
blue,
yellow,
orange,
violet,
END OF ENUM ecolour STRUCTURE color.

IF new_color = color-red.

ENDIF.
“(C) Brandeis Consulting”

Manual value assignment - VALUE

Fixed values can be assigned to constants manually. This is particularly important if the values are persisted somewhere or if existing applications are to be migrated.
Another good reason may be that you want to specify the order when sorting by value.

TYPES: BEGIN OF ENUM ecolour ,
red VALUE 0,
green VALUE 1,
blue VALUE 2,
yellow VALUE 3,
orange VALUE 4,
violet VALUE 5,
END OF ENUM ecolour.

“(C) Brandeis Consulting”

Typed enumerations - BASE TYPE

ABAP normally uses the type i internally for enumerations. If character-type values are to be assigned manually, the data type must be set accordingly.

TYPES toption TYPE C LENGTH 2.

TYPES: BEGIN OF ENUM eoption BASE TYPE toption,
eq VALUE 'EQ',
bt VALUE 'BT',
le VALUE 'LE',
lt VALUE 'LT',
...
END OF ENUM eoption.

There is a maximum length of 8 characters for character strings ! See example task status.
“(C) Brandeis Consulting”

Accessing the value

The value of an enumeration object cannot be accessed directly. Instead, an assignment or output returns its identifier. SAP Documentation

Conversion to the value

When the value is written to the database, the type does not match and must be converted.

variable_of_basetype = CONV base_type( enum_value ).{abap}

Conversion to the enumeration

Conversely, the value in the database must be converted back to a suitable enumeration type:

variable_of_enumtype = CONV enum_type( basetype_value ).

“(C) Brandeis Consulting”

Demo/example: Accessing the value

TYPES: BEGIN OF ENUM ebool
BASE TYPE char1,
true VALUE 'X',
false VALUE IS INITIAL,
END OF ENUM ebool.
...
METHOD if_oo_adt_classrun~main.
out->write( |Value without CONV: { true } | ).
out->write( |Value with CONV: { CONV char1( true ) }| ).
ENDMETHOD.

Output

Value without CONV: TRUE
Value with CONV: X
“(C) Brandeis Consulting”

Recommendation for working with constants

Enumerations and domains must match 1:1. Since the enumeration is not a DDic type, this cannot be guaranteed 100%.

The book Clean ABAP therefore largely advises against using ENUM because, in theory, contradictions with domain definitions are possible.

I consider the complexity of the proposed alternatives to be too high.

“(C) Brandeis Consulting”

Clean code recommendations

  • For string enumerations: The value should always be identical to the constant.
  • No collection classes or interfaces that contain many constants or enumerations. Example: ZCL_FICO_CONSTS
    The definition of enumerations belongs where you would expect to find them.
“(C) Brandeis Consulting”