Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Assignments →  Assignment and Conversion Rules 

Conversion Rules for Enumerated Types

An enumerated type is a special elementary data type. Variables of this type are enumerated variables, which can only contain the enumerated values that are defined for the enumerated type. For this reason, source fields and target fields must be fully compatible in assignments between enumerated types, with one exception. The compatibility applies to the enumerated type itself. The base type of the enumerated type is not relevant.

The only exception to this rule is the assignment of source fields with enumerated types to character-like variables of the types c and string. In this case, the following conversion rule applies:

The name (in uppercase) of the enumerated constant or the component of the enumerated structure in which the current value of the source field is saved during the definition of the enumerated type, and the length of the name, are assigned to the target field.

There is no reverse conversion rule for assignments between character-like source fields and enumerated variables.

Other versions: 7.31 | 7.40 | 7.54


Notes

  • The following enumerated objects are possible as operands in assignments between enumerated types:

  • To be compatible, the source field and target field must have exactly the same enumerated type. An enumerated type created using RTTC methods is never identical to a type defined using TYPES. A type description object created for an existing enumerated type using RTTC methods, however, does describe this type.

  • Enumerated objects with enumerated types can be used in all reading positions where an implicit conversion to a character-like data type c or string takes place. They cannot be used in operand positions where the operand has to have a character-like data type.

  • When converted to a character-like type, the direct name of the enumerated constant is always used in the context of the declaration with a maximum of 30 characters. It is not prefixed with the name of an enumerated structure with the structure component selector (-), nor with any class name with the class component selector (=>). Therefore, the conversion of enumerated objects of different enumerated types can have the same result if the names of the enumerated constants match.

  • To assign a type-dependent initial value to enumerated variables, the statement CLEAR or a constructor expression VALUE #( ) can be used on the right side of an assignment.

Example

The enumerated values of the enumerated structure p of an enumerated type planet are loaded into an internal table whose row type is the enumerated type. Each row is then converted to the character-like type string and this is added to the internal table planet_names.

TYPES: 
  BEGIN OF ENUM planet STRUCTURE p, 
    mercury, 
    venus, 
    earth, 
    mars, 
    jupiter, 
    saturn, 
    uranus, 
    neptune, 
  END OF ENUM planet STRUCTURE p. 

DATA planets TYPE SORTED TABLE OF planet 
             WITH UNIQUE KEY table_line. 
DO. 
  ASSIGN COMPONENT sy-index OF STRUCTURE p TO FIELD-SYMBOL(<p>). 
  IF sy-subrc <> 0. 
    EXIT. 
  ENDIF. 
  planets = VALUE #( BASE planets ( <p> ) ). 
ENDDO. 

DATA planet_names TYPE SORTED TABLE OF string 
                 WITH UNIQUE KEY table_line. 
planet_names = VALUE #( 
  BASE planet_names FOR enum IN planets ( CONV string( enum ) ) ). 

cl_demo_output=>display( planet_names ).