ABAP - Keyword Documentation →  ABAP - Reference →  Processing Internal Data → 

Enumerated Objects

This section summarizes the use of enumerated objects (data objects with enumerated types.

Executable Example

Enumerated Objects, Use

Declaration

An enumerated object is a data object with an enumerated type defined by one of the following TYPES statements:

TYPES BEGIN OF ENUM enum_type ...
  TYPES val1 ...
  TYPES val2 ...
  ...
TYPES END OF ENUM enum_type ...

The technical data type of the content of an enumerated object is the base type of the enumerated type. The potential content is defined by the enumerated values defined using TYPES val1, TYPES val2, ..., of which at least one value must have the type-friendly initial value. The base type is i by default but it can be a different elementary data type. The following enumerated objects exist:

An enumerated variable is a variable defined using
DATA enum_var TYPE enum_type ...
that can contain only enumerated values of the enumerated type. This is ensured by the ABAP runtime environment and the rules for using enumerated types.
An enumeration constant is used to define a value in the value set of an enumerated type. In the definition of an enumerated type, it is defined using
TYPES val ...
under the name val. This constant is a constant of the context of its definition and contains the enumerated value assigned in the definition. It can be specified in all reading positions in which enumerated objects are possible. Its enumerated value is either determined automatically as a whole number or can be specified explicitly in the definition.
A component of an enumeration structure is a special form of an enumeration constant that exists as a component of a constant structure and not as a single data object. The enumeration structure struc is defined using
TYPES BEGIN OF ENUM enum_type STRUCTURE struc ...
. This makes the enumeration constants defined using
TYPES val ...
into their structure components. Otherwise, the same applies as to regular enumeration constants.

Notes

Example

Declares an enumerated type size in a class. The method parameter size has the enumerated type and only enumerated objects of this type can be passed to this parameter. This example show how the enumeration constant demo=>l is passed. This guarantees that only enumerated values of the enumerated type can be passed to the parameter. These values can be evaluated in comparisons with the enumeration constants. In the CASE control structure shown here, the statement block after WHEN OTHERS can be reached only when demo=>xl and demo=>xxl are passed.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    TYPES:
      BEGIN OF ENUM size,
        s, m, l, xl, xxl,
      END OF ENUM size.
    CLASS-METHODS main
      IMPORTING size TYPE size.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    CASE size.
      WHEN s.
        ...
      WHEN m.
        ...
      WHEN l.
        ...
      WHEN OTHERS.
        ...
    ENDCASE.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( demo=>l ).

Processing of Enumerated Objects

Enumerated objects are almost always processed independently of the base type of the enumerated type. Only the enumerated type itself is of relevance for all rules specified here. Assignments of enumerated objects with a numeric base type, for example, cannot be assigned to numeric target fields and cannot be compared with numeric fields. The enumerated vale in the base type can be accessed using the constructor operators CONV and EXACT only.

Operand Positions for Enumerated Objects

Enumerated objects can be used in all reading positions in which the operand type is their enumerated type or in which the operand is converted to one of the character-like types c or string. Substring access is not possible.
Enumerated variables can only be used in writing positions in which the operand type is the enumerated type and only the associated enumerated values can be written. If known statically, an attempt to assign a value other than a valid enumerated value to an enumerated variable produces a syntax error. If not known statically, an exception is raised.

Example

In the first assignment, the enumeration constant xl in a reading position is assigned to the enumerated variable size in a writing position. The string expression in the second half exploits the fact that the enumeration constants are converted implicitly to the type string before the chaining. The result is SMLXLXXL.

TYPES:
  BEGIN OF ENUM size,
    s, m, l, xl, xxl,
  END OF ENUM size.

DATA size TYPE size.

size = xl.

DATA(str) = s && m && l && xl && xxl.

Value Assignments

Only enumerated objects with the same enumerated type can be assigned to an enumerated variable. In the assignment, the target field is given the enumerated value of the source field.

The initial value of the base type is always a valid enumerated value of an enumerated type. Accordingly, an enumerated variable can be set to the initial value of its base type using CLEAR. Assignments of VALUE enum_type( ) are also possible.

In the reverse case, enumerated objects can only be assigned to compatible enumerated variables, with the following exception: There is a conversion rule for assignments of enumerated objects to character-like variables of the types c and string. In this case, the target field is assigned the name of the enumeration constant or of the component of the enumeration structure under which the enumerated value of the source field is defined in the enumerated type-

In structures, each component comprises a separate fragment of the Unicode fragment view using an enumerated type. In assignments between structures of this type, the fragment views must match. This makes sure that only components with the same enumerated type can be assigned to each other.

Example

In the first assignment, the enumeration constant sz-xl is assigned to the enumerated variable size of its enumerated type. This variable then contains the associated enumerated value 3. In the second assignment, the enumeration constant is assigned to the text string size_string. This string is given the value XL in accordance with the conversion rule.

TYPES:
  BEGIN OF ENUM size STRUCTURE sz,
    s, m, l, xl, xxl,
  END OF ENUM size STRUCTURE sz.

DATA size TYPE size.
size = sz-xl.

DATA size_string TYPE string.
size_string = sz-xl.

Comparisons

In comparisons between enumerated objects, the comparison rule applies that an enumerated object can only be compared with an enumerated object with the same enumerated type. Here, the values of the operands are compared in accordance with their base type.

Each enumerated type has an initial enumerated value, which makes checks with the predicate expression IS INITIAL possible.

Example

The first comparison shows a typical case where an enumerated variable is compared with an enumeration constant. The syntax of the second comparison (in a comment) is not possible. In the third comparison, the enumerated variable is converted explicitly to the type string before the comparison with a text string.

TYPES:
  BEGIN OF ENUM size,
    s, m, l, xl, xxl,
  END OF ENUM size.

DATA size TYPE size.

...

IF size = xl.
ENDIF.

"IF size = `XL`. "<--- Syntax error
"ENDIF.

IF CONV string( size ) = `XL`.
ENDIF.

Typing of Formal Parameters and Field Symbols

If formal parameters of procedures or field symbols are typed with an enumerated type, only enumerated objects with the same enumerated type can be assigned to them. As usual, an exception to this are return values of functional methods that can also be assigned to character-like objects of the types c and string.

Enumerated types are covered by the generic types any, data, and simple. When an enumerated object is passed to generically typed formal parameters or in assignments to generically typed field symbols, these are given the enumerated type. In assignments to field symbols, castings with the CASTING addition are not possible and an enumerated type cannot be specified after this addition.

When generically typed formal parameters or field symbols are used for enumerated objects, the restriction applies that only statically known operands with the same enumerated type are permitted in reading positions in which an enumerated object is expected and this is known statically. This affects, for example, the source field of an assignment to an enumerated variable or an operand compared with an enumerated object. In writing positions for enumerated objects, however, generic formal parameters or field symbols are allowed for enumerated types. If the operand type is not known statically, the check is only made at runtime in reading positions too.

Example

The field symbol fs1 typed generically with simple cannot be assigned to an enumerated variable size known statically or compared with it. An assignment of size to the field symbol and fully generic handling are, however, possible.

TYPES:
  BEGIN OF ENUM size,
    s, m, l, xl, xxl,
  END OF ENUM size.

FIELD-SYMBOLS <fs1> TYPE simple.
FIELD-SYMBOLS <fs2> TYPE simple.

DATA(size) = xl.

ASSIGN size TO <fs1>.
ASSIGN size TO <fs2>.

<fs1> = size.
"size = <fs1>.        "<--- Syntax error
"ASSERT size = <fs1>. "<--- Syntax error

<fs2> = <fs1>.
ASSERT <fs1> = <fs2>.

Access to the Enumerated Value

A special rule for the conversion operator CONV applies when accessing the enumerated value of an enumerated object:

... CONV base_type( enum_dobj ) ...

When the base type base_type of an enumerated object enum_dobj specified as an argument is specified directly or indirectly, CONV returns its enumerated value.

In the reverse case, a valid enumerated value can be converted to an enumerated object:

... CONV enum_type( dobj ) ...

The argument dobj is converted to the base type of the enumerated type enum_type and CONV returns an enumerated object with this value. Any invalid values raise an exception.

In combinations of these two variants (in which CONV base_type( enum_dobj ) is used as an argument dobj of CONV enum_type( dobj )), there is a short form:

... CONV enum_type( enum_dobj ) ...

If different enumerated types can have the same base type, an enumerated object of an enumerated type can be converted to the corresponding enumerated object of a different enumerated type.

Note

The corresponding rules applies to the lossless operator EXACT. Here, additional losslessness checks are made.

Example

The inner conversion operator CONV accesses the current enumerated value of the enumerated object size and returns it in the type i. The outer conversion operator CONV converts the result of the addition back to an enumerated type and assigns this enumerated value to the enumerated variable size. The enumerated value in size is raised by one in each iteration. The final result is the value of the enumeration constant xxl.

TYPES:
  BEGIN OF ENUM size,
    s, m, l, xl, xxl,
  END OF ENUM size.

DATA size TYPE size.

DO 4 TIMES.
  size = CONV #( CONV i( size ) + 1 ).
ENDDO.

Type Descriptions

The type returned for an enumerated object by the statement DESCRIBE FIELD is k. The length is the length of the enumerated value in the base type in bytes.

In RTTS, enumerated objects are described by objects of the class CL_ABAP_ENUMDESCR. This class can be used for RTTI and RTTC purposes. Like any enumerated type, an enumerated type created using RTTC is only compatible with itself.

The following attributes exist in a type description of the class CL_ABAP_ENUMDESCR:

Executable Example

Enumerated Objects, Type Description

Data interfaces

The following data interfaces support enumerated types:

The character-like representation of enumerated objects (namely the result of a c or string) is used for their output and serialization. The output consists of the name (with a maximum of thirty characters) of the enumeration constant of the current enumerated value in uppercase. Deserializations are performed in the reverse direction. The following are supported:

Note

A deserialization of a name of an enumeration constant is one of the few ways of creating an enumerated value from the name in full and dynamically.

Example

WRITE output of the enumeration constants of an enumeration structure.

TYPES:
  BEGIN OF ENUM size STRUCTURE sz,
    s, m, l, xl, xxl,
  END OF ENUM size STRUCTURE sz.

WHILE sy-subrc = 0.
  ASSIGN COMPONENT sy-index OF STRUCTURE sz TO FIELD-SYMBOL(<fs>).
  IF sy-subrc = 0.
    WRITE / <fs>.
  ENDIF.
ENDWHILE.

Executable Example

Enumerated Objects, Deserialization

Forbidden Uses

The following uses are forbidden to ensure that an enumerated object only ever contains a single valid enumerated value:

If an enumerated object is provided with an invalid value due to a gap in the rules, this results in the following behavior:

An invalid value must be viewed as an error and should never occur.

Note

Classic Dynpros represent a known gap. Enumerated types are not supported by dynpros. When dynpro input fields are taken from a program by using enumerated objects, they are handled like an object of the base type. Any invalid values are passed to the associated enumerated object in the event PAI without being checked. For this reason, enumerated objects should never be associated with classic dynpros.

Example

The program DEMO_ENUM_DYNPRO uses an enumerated object on a dynpro. This can result in invalid values in the program.



Continue
Example Enumerated Objects, Use
Example Enumerated Objects, Type Description
Example Enumerated Objects, Deserialization