ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data
Enumerated Objects
This section summarizes the use of enumerated objects (data objects with enumerated types.
Other versions:
7.31 | 7.40 | 7.54
Executable Example
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-dependent
initial value. The base type is i
by default but it can be a different elementary data type. The following enumerated objects exist:
- Enumerated variables
DATA enum_var TYPE enum_type ...
- Enumeration constants
TYPES val ...
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.
- Components of enumeration structures
struc
is defined using
TYPES BEGIN OF ENUM enum_type STRUCTURE struc ...
TYPES val ...
Notes
- Enumerated objects are mainly used to check permitted values. This usually restricts the actual parameters passed to methods to the enumerated values defined in the class.
- The base type and the actual enumerated value are almost always ignored when enumerated objects are used. Assignments and comparisons are usually only made between enumerated objects with the same enumerated type.
- Base types other than
i
and the actual enumerated values may be significant in transformations between enumerated type or in migrations of predecessor concepts.
- Enumeration structures can be used to avoid naming conflicts if there are multiple enumerated types in a single namespace. Enumeration structures can be used to enable the use of the same enumeration constant name in multiple enumerated types.
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
- Reading positions
c
or string
.
Substring access is not possible.
- Writing positions
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:
- KIND always has the value E for the elementary base type
- TYPE_KIND always has the value k (as in the statement
DESCRIBE FIELD
)
- BASE_TYPE_KIND describes the base type
- MEMBERS is a table of the enumeration constants and the associated enumerated values
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:
- Data clusters with
the statements
EXPORT
andIMPORT
. When an enumerated object is exported, the enumerated value is saved in the base type and flagged as an enumerated value. Both enumerated objects and exported data objects of the base type can be imported to a suitable enumerated object (the value is checked here). No exported enumerated objects, however, can be exported to data objects of the base type.
- ABAP file interface
with the statements
TRANSFER
andREAD DATASET
. In writes to a file and reads from a file, enumerated objects are handled like data objects of its base type. In reads from a file to an enumerated object, the value is checked to see whether it is a valid enumerated value.
- Serializations and deserializations from and to XML and JSON. The formats asXML and asJSON represent the content of enumerated objects in their character-like representation (the name of the enumeration constant of the current enumerated value). Only valid names are allowed in deserializations to an enumerated object.
- List output with the statement
WRITE
. LikeWRITE TO
, this statement converts an enumerated object to its character-like representation (the name of the enumeration constant of the current enumerated value). The output length is the same as the maximum length of the name (30 characters).
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:
- Within ABAP, enumerated objects are never interpreted in accordance with their base type. This means that they cannot be used in operand positions that expect numeric, character-like, or byte-like data types. The only exception to this are the operand positions in which an implicit conversion to a character-like type takes place.
- Enumerated types are not currently supported by ABAP Dictionary. Accordingly, no database tables whose columns have an enumerated type can be defined in ABAP Dictionary.
- In both ABAP SQL and Native SQL (
EXEC SQL
, ADBC), no host variables or references to ABAP variables with enumerated type can be used. The actual parameters of AMDP methods cannot be typed using enumerated types.
- No enumerated types are supported for the input fields of selection screens.
- It is not possible to use enumerated values from lists in the list buffer, since there are no conversion rules between character-like types and enumerated types.
If an enumerated object is provided with an invalid value due to a gap in the rules, this results in the following behavior:
- The result of a conversion to
c
orstring
is the string <illegal enum value>, which is displayed accordingly in the ABAP Debugger.
- An enumerated object with an invalid value can be assigned to other enumerated objects with the same enumerated type without being checked.
- The expressions CONV base_type( enum_dobj
) and
EXACT base_type( enum_dobj )
return the invalid value.
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 produce invalid values in the program.