Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Attributes of Data Objects →  DESCRIBE →  DESCRIBE FIELD 

Determining Elementary Data Types

This example demonstrates how the characteristics of elementary data types can be determined during 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,
          mess    TYPE string.

    TRY.
        CREATE DATA: dref1 TYPE (type1),
                     dref2 TYPE (type2).

        ASSIGN: dref1->* TO <data1>,
                dref2->* TO <data2>.

      CATCH cx_sy_create_data_error.
        MESSAGE 'Create data error!' TYPE 'I' DISPLAY LIKE 'E'.
        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.
        mess = `Assignment from type ` &&
               tdescr2                 &&
               ` to `                  &&
                tdescr1                &&
               ` not allowed!`.
        MESSAGE mess TYPE 'I' DISPLAY LIKE 'E'.
    ENDTRY.

Description

This example implements a type check that will only allow 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 CREATE DATA statement, to dynamically create anonymous data objects of the types specified. If this does not work, an error message is displayed.

The generated data objects are assigned to the field symbols <data1> and <data2>.The DESCRIBE FIELD statement is then used to determine the type of the dynamically created data object. Only in the case of both data objects having the same type is <data1> assigned to <data2>.

The example will not work if you enter complex data types such as SCARR and SPFLI into the input fields. In this case DESCRIBE FIELD, ("u" in Unicode or "C" in non-Unicode programs) declares the same type, which can cause runtime errors, if the structures are not convertible.

Use the RTTS methods to check complex data types and object types during runtime.