Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Assignments →  Assignment Rules →  Assignment Rules for Reference Variables 

Assignments between Data Reference Variables

Data reference variables are either completely typed or typed with the generic type data.

Other versions: 7.31 | 7.40 | 7.54

Up Cast in Data References

An up cast in data references is possible in the following cases:

  • The static types of the source variable and the target variable match according to the following rules:
  • Both are elementary data types with identical technical type attributes, that is predefined ABAP type, length and number of decimal places. It is of no consequence how the static types have been defined.
  • Both have the same structured type. For structured types, identical technical type attributes are not sufficient. In the definition of the static types, the same structured type must have been used.
  • Both are table types with matching technical type attributes, that is row types, table category and table key. With non-structured row types, identical technical attributes of the row type are sufficient. With structured row types, both definitions must contain the same structured type.
  • The static type of the source variable is completely typed and that of the target variable is generic.


Note

Concerning the check of static types, structured types behave like classes. Two differently structured types do not match, even if they consist of identical components.

Down Cast in Data References

A down cast in data references is only possible if the static type of the source variable is generic and that of the target variable is completely typed. The syntax check precludes that the static types of the source variable and the target variable are both completely typed, but not identical.


Example

Assigning from dref1 to dref2 is an up cast. Assigning from dref2 to dref1 is a down cast, which in this example below leads to an exception. If the statement CREATE DATA had the addition TYPE i, the down cast would also have been successful.

DATA: dref1 TYPE REF TO i, 
      dref2 TYPE REF TO data. 

CREATE DATA dref1. 
dref2 = dref1. 
CREATE DATA dref2 TYPE string. 

TRY. 
  dref1 ?= dref2. 
  CATCH cx_sy_move_cast_error. 
  ... 
ENDTRY.