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:
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:
- Source fields can be enumerated constants or components of enumerated structures and enumerated variables.
- Target fields can be enumerated variables.
- 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
orstring
takes place. They cannot be used in operand positions where the operand has to have a character-like data type.
- The ABAP Debugger displays the name of the value, converted into a character-like type, for an enumerated object.
- 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.
- The actual value of an enumerated object can be read, according to its
base type, using the conversion operator
CONV
.
- To assign a type-dependent initial value to enumerated variables, the statement
CLEAR
or a constructor expressionVALUE #( )
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 ).