SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
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.
Source Code
REPORT demo_describe_field.
CLASS conv_exc DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
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.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
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.