ABAP Keyword Documentation → ABAP - Reference → Processing Internal Data → Attributes of Data Objects → DESCRIBE → DESCRIBE FIELD
Determining Elementary Data Types
This example demonstrates how the attributes of elementary data types can be determined at runtime.
Other versions: 7.31 | 7.40 | 7.54
Source Code
DATA: dref1 TYPE REF TO data,
dref2 TYPE REF TO data.
FIELD-SYMBOLS: <data1> TYPE any,
<data2> TYPE any.
DATA: tdescr1 TYPE c LENGTH 1,
tdescr2 TYPE c LENGTH 1.
DATA: type1 TYPE c LENGTH 30 VALUE 'I',
type2 TYPE c LENGTH 30 VALUE 'C'.
cl_demo_input=>add_field( CHANGING field = type1 ).
cl_demo_input=>request( CHANGING field = type2 ).
TRY.
CREATE DATA: dref1 TYPE (type1),
dref2 TYPE (type2).
ASSIGN: dref1->* TO <data1>,
dref2->* TO <data2>.
CATCH cx_sy_create_data_error.
cl_demo_output=>display( 'Create data error!' ).
LEAVE PROGRAM.
ENDTRY.
DESCRIBE FIELD: <data1> TYPE tdescr1,
<data2> TYPE tdescr2.
TRY.
IF tdescr1 <> tdescr2.
RAISE EXCEPTION TYPE conv_exc.
ELSE.
<data2> = <data1>.
ENDIF.
CATCH conv_exc.
cl_demo_output=>display( `Assignment from type ` &&
tdescr2 &&
` to ` &&
tdescr1 &&
` not allowed!` ).
ENDTRY.
Description
This example implements a type check that only allows assignments to be made if the source and target fields have the same type.
Using the two input fields type1
and type2
, the
names of elementary data types are entered. First an attempt is made, using the statement CREATE
DATA, to create anonymous data objects of the types specified dynamically. If this does not work, an error message is displayed.
The created data objects are assigned to field symbols <data1>
and
<data2>. Then, the statement DESCRIBE FIELD
determines the type
of the data objects created dynamically. Only if both data objects have the same type is <data1>
assigned to <data2>
.
The example does not work if complex data types such as SCARR and SPFLI are entered in the input fields. In this case, DESCRIBE FIELD
defines identical types ("u" in Unicode programs or "C" in
obsolete
non-Unicode programs), which can cause runtime errors if the structures cannot be converted to each other.
The RTTS methods can be used to check complex data types and object types at runtime.