sapdev logo background
sapdev logo sapdev logo
Comments

ABAP MOVE CAST Statement syntax, information and example SAP source code



Return to Statement index



=, ?= - Up Cast and Down Cast

ABAP Syntax destination_ref =|?= source_ref.

What does it do? Assignment between two reference variables. The reference in source_ref is assigned destination_ref . If the assignment is successful, destination_ref points to the same object as source_ref (
reference semantics ). The assignment of reference variables is a special form of assignments of data objects ; assignments between reference variables require two assignment operators, used in accordance with the
assignment rules for reference variables :
In assignments between reference variables, the assignment operator = can only be used for up cast s in which the static type of source_ref is more specific than or the same as the static type of destination_ref .
The special casting operator
?= can only be used for assignments between reference variables. If the static type of source_ref is more general than the static type of destination_ref , ?= must be used to produce a down cast . If this is statically identifiable, it is checked by the syntax check; if not, it is checked at runtime. The actual down cast (that is, the check to see whether assignments are possible in accordance with the assignment rules for reference variables) never takes place until runtime. If, then, the static type of
destination_ref is not more general or is the same as the dynamic type of source_ref , a handleable exception is raised and the target variable keeps its original value.
The same applies to the right side and left side as when assigning data objects , with the following restrictions:
The data types of the source and target are reference types , which means that functional method calls and constructor expressions and table expressions can be specified on the right side whose return value has a reference type. Predefined functions and calculation expressions do not return any reference variables and are not possible here.
An inline declaration DATA(var) is possible only on the left side of = , and not on the left side of ?= . The static types of the reference variable source_ref
is used, which must be statically identifiable.
Latest notes: The casting operator ?= can always be specified, even for up casts. This is, however, not usually necessary.
Alongside ?= , the casting operator CAST also enables down casts to operand positions, which saves on helper variables.
The casting operator ?= cannot be used in multiple assignments .
One obsolete form of down cast is the statement
MOVE with the addition ?TO .
Example ABAP Coding The first two assignments in the following source code section are up casts:
The instance operator NEW creates a result with the static and dynamic type c2 , which can be assigned to the more general reference variable oref1 .
Any reference variable can be assigned to the reference variable
oref with the most general static type object .
The next two assignments are down casts:
It is not possible to test whether the general reference variable
oref points to an object that can also point to oref2 until runtime. This is the case in the example.
The down cast of oref2 to oref3 , however, fails at runtime and raises the caught exception.
CLASS c1 DEFINITION INHERITING FROM object.
ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1.
ENDCLASS.

CLASS c3 DEFINITION INHERITING FROM c2.
ENDCLASS.

DATA oref TYPE REF TO object.
DATA oref1 TYPE REF TO c1.
DATA oref2 TYPE REF TO c2.
DATA oref3 TYPE REF TO c3.

oref1 = NEW c2( ).

oref = oref1.

TRY.
oref2 ?= oref.
CATCH cx_sy_move_cast_error.
...
ENDTRY.

TRY.
oref3 ?= oref2.
CATCH cx_sy_move_cast_error.
...
ENDTRY.



Runtime Exceptions

Catchable Exceptions
CX_SY_MOVE_CAST_ERROR
Reason for error: Type conflict in down cast
Runtime error: MOVE_CAST_ERROR
Reason for error: Source variable or target variable is not a reference variable
Runtime error: MOVE_CAST_REF_ONLY
Reason for error: Dynamic type conflict in assignment of references
Runtime error: MOVE_CAST_ERROR_DYN
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved








comments powered by Disqus