ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Assignments → CONV - Conversion Operator
CONV - Conversion of Enumerated Types
If the constructor expression CONV is used on
enumerated types, the following rules apply:
- If the argument
dobjis an enumerated objectenum_dobjandtypecorresponds exactly to the base typebase_typeof the enumerated type, the result is the actual enumerated value of the argument.
... CONV base_type( enum_dobj ) ...
- If
typeis an enumerated typeenum_type, the following can be specified for the argumentdobj:
- A data object of the same elementary type as the base type of the enumerated type, with any length and number of decimal places
- An expression permitted at this position whose result can be converted to the base type of the enumerated type
... CONV enum_type( dobj ) ...
In all other cases, the normal conversion rules
also apply for CONV, where enumerated types can only be converted to the
character-like types c and string; no other types are converted to enumerated types.
Other versions:
7.31 | 7.40 | 7.54
Short form
If the enumerated type of an enumerated object enum_dobj has the same base
type base_type as any other enumerated type enum_type
and the enumerated value in the enumerated variable is also defined in enum_type,
it is possible, according to the above rules, to nest constructor expressions CONV as follows:
CONV enum_type( CONV base_type( enum_dobj ) )
The following short form can also be used for the nesting:
CONV enum_type( enum_dobj )
The result is the enumerated value from enum_dobj with the data type enum_type.
Notes
- Using
CONVfor enumerated types deviates from the rule thatCONValways behaves according to the normal conversion rules in assignments.
- If the argument is an enumerated type with a character-like
base type, and this base
type is specified as
type, the special rule above applies instead of the general conversion rule. That is, the enumerated value is returned, not the name.
Example
The first three assignments to the fields text1, text2, and text3 follow the normal
conversion rules for enumerated type to character-like. The result in each case is the name BLUE.
A direct assignment of the enumerated constant blue to a field value1
of type i is not possible. The conversion operator CONV
returns the actual enumerated value 1 for the base type i.
It is also not possible to assign the value 1 of type i to an enumerated
variable color1 with enumerated type color. However,
the conversion operator CONV returns an enumerated value for the type color if the argument has the base type i and a permitted value.
TYPES:
BEGIN OF ENUM color,
red, blue, green,
END OF ENUM color.
DATA: text1 TYPE string,
text2 TYPE string.
text1 = blue.
text2 = CONV #( blue ).
DATA(text3) = CONV string( blue ).
ASSERT text1 = text2.
ASSERT text1 = text3.
DATA: value1 TYPE i,
value2 TYPE i.
"value1 = blue.
value2 = CONV #( blue ).
DATA(value3) = CONV i( blue ).
ASSERT value2 = value3.
DATA: color1 TYPE color,
color2 TYPE color.
"color1 = 1.
color2 = CONV #( 1 ).
DATA(color3) = CONV color( 1 ).
ASSERT color2 = color3.
cl_demo_output=>display( |Name: { color3
}\nValue: { value3 }| ).
Example
The short form
CONV animal( peach )
of the conversion operator CONV acts like
CONV animal( CONV i( peach ) )
It generates the value 2 from the enumerated constant peach of the enumerated
type fruit, which can be assigned to the enumerated variable animal
with enumerated type animal. The output shows the associated name DOG.
TYPES:
BEGIN OF ENUM fruit,
apple, peach, orange,
END OF ENUM fruit.
TYPES:
BEGIN OF ENUM animal,
cat, dog, cow,
END OF ENUM animal.
cl_demo_output=>display( CONV animal( peach ) ).
Example
The example shows the difference between the normal conversion rules and using CONV for a character-like base type:
- When the assignment is to
result1, the normal conversion rules apply and the name SECOND is assigned.
- When the assignment is to
result2, the special rule forCONVapplies. The base type is specified and the enumerated value aaaaaaaa is assigned.
TYPES:
char8 TYPE c LENGTH 8,
BEGIN OF ENUM text BASE TYPE char8,
first VALUE IS INITIAL,
second VALUE 'aaaaaaaa',
third VALUE 'bbbbbbbb',
END OF ENUM text.
DATA: result1 TYPE char8,
result2 TYPE char8.
result1 = second.
result2 = CONV #( second ).
cl_demo_output=>display(
|result1: { result1 }\nresult2: { result2 }| ).
Executable Example
Conversion Operator, Enumerated Types