Assigning Values with MOVE
To assign the value of a data object source to a variable destination, use the following statement:
MOVE source TO destination.
or the equivalent statement
destination = source.
The content of source remains unchanged, source does not therefore have to be a variable - it can also be a literal, a text symbol, or a constant. You must always specify decimal points with a period (.), regardless of the user’s personal settings.
f4 = f3 = f2 = f1.
are also possible. ABAP processes them from right to left as follows:
MOVE f1 TO f2.
MOVE f2 TO f3.
MOVE f3 TO f4.
In the MOVE statement (or when you assign one value to another with the equal sign), it is not possible to specify the field names dynamically as the contents of other fields. If you need to do this, you must use field symbols .
The source and target fields can be of different data types. The result of the value assignment depends on whether these data types are compatible and whether a type conversion can be performed. If there is no conversion rule between the data types in question, no assignment can be made.
DATA: t(10) TYPE c,
number TYPE p DECIMALS 2,
count TYPE i.
t = 1111.
MOVE '5.75' TO number.
count = number.
Following these assignments, the fields t, number and count have the values ‘1111 ’, 5.75, and 6 respectively. When you assign the number literal 1111 to T, it is converted into a character field with length 10. When you assign number to count , the decimal number is rounded to an integer (as long as the program attribute Fixed pt. arithmetic has been set).
Assigning Values Between Components of Structures
The rules for value assignments between data objects also apply to structures. With the command
DATA: struct1 TYPE structure,
struct2 TYPE structure.
struct1 = struct2.
two structures of the same type can be assigned to one another without difficulty. Here, the entire source structure is seen as a unit and copied to the source structure. It is then possible to access the components individually again. If the structures in question are not compatible, see the conversion rules for structures.
In practice, however, you will often only need to assign certain components of a structure to be certain components of another structure. ABAP has a special statement for this purpose:
MOVE-CORRESPONDING sourcestruct TO destinationstruct.
This statement assigns the contents of the components of structure sourcestruct to the components of the destinationstruct structure that have identical names.
When it is executed, it is broken down into a set of MOVEstatements, one for each pair of fields with identical names, as follows:
MOVE sourcestruct-comp1 TO destinationstruct-comp1.
MOVE sourcestruct-comp2 TO destinationstruct-comp2.
Any necessary type conversions are performed individually.
DATA: BEGIN OF address,
firstname(20) TYPE c VALUE 'Fred',
surname(20) TYPE c VALUE 'Flintstone',
initials(4) TYPE c VALUE 'FF',
street(20) TYPE c VALUE 'Cave Avenue',
number TYPE i VALUE '11',
postcode(5) TYPE n VALUE '98765',
city(20) TYPE c VALUE 'Bedrock',
END OF address.
DATA: BEGIN OF name,
surname(20) TYPE c,
firstname(20) TYPE c,
initials(4) TYPE c,
title(10) TYPE c VALUE 'Mister',
END OF name.
MOVE-CORRESPONDING address TO name.
In this example, the values of name-surname, name-firstname and name-initials are set to 'Flintstone’, ‘Fred’, and 'FF'. name-title always has the value ‘Mister’.
SAP NetWeaver AS ABAP Release 752, ©Copyright 2017 SAP AG. All rights reserved.ABAP - Keyword Documentation → ABAP - Reference → Processing Internal Data → Assignments → Assigning References → Setting Reference Variables →
Assignment Rules for Reference Variables
The content of a reference variable can only be assigned to another reference variable. At the same time,
. No conversion takes place when variables are assigned. For an assignment to take place, the static type of the target reference variable must be more general than or match the dynamic type of the source reference variable. If the assignment is successful, the target reference variable points to the same object as the source reference variable, meaning that the target reference variable inherits the dynamic type of the source reference variable.
Reference variables can point to objects in the internal session or in the shared objects memory. An area handle is used to bind an area instance version to a program, however references from the program can be made to shared objects and vice versa (if these are read or write references). In other cases, an area instance version is closed, which means that no references can point to the internal session from an area instance version. References that point from the internal session to shared objects can be saved, but not dereferenced. Object references and data references are possible within a completed area instance version, although data references are subject to restrictions with respect to the dynamic type.
Static Type and Dynamic Type
Each reference variable has a dynamic type and a static type.
- The dynamic type is defined at runtime of the program, and is the data type of the data object or the class of the object to which the reference variable points. It determines the components contained in the object.
- The static type is set with the declaration of the reference variable. In data references, the static type is either a non-generic data type or the predefined generic type data. In object references, the static type is either a class or an interface, so an object reference can also be referred to as a class reference or an interface reference.
The static type of a reference variable is always less specific or the same as the dynamic type.
An initial reference variable contains the null reference, which does not point to any objects. It therefore does not have a data type or a class as a dynamic type. The same applies to a non-initial reference variable containing an invalid reference that no longer points to an object.
The object reference variable oref has the static type super_class. After the object is generated with NEW, its dynamic type is the special class sub_class - as shown by RTTI.
CLASS super_class DEFINITION.
CLASS sub_class DEFINITION INHERITING FROM super_class.
DATA oref TYPE REF TO super_class.
oref = NEW sub_class( ).
cl_demo_output=>display( CAST cl_abap_classdescr(
cl_abap_refdescr=>describe_by_object_ref( oref ) )->absolute_name ).
Up Cast and Down Cast
In an assignment between reference variables, the target variable applies the dynamic type of the source variable. An assignment is possible if the static type of the target variable is less specific or the same as the dynamic type of the source variables.
If the static type of the target variables is less specific or the same as the static type of the source variable, assignment is always possible. The name up cast arises from the fact that the movement within the inheritance space is upwards. Since the target variable can accept more dynamic types in comparison to the source variables, this assignment is also known as a widening cast. An up cast is possible in all ABAP statements in which the content of a data object is assigned to another data object. This includes, for example, assignments with the normal assignment operator (=), the insertion of rows in internal tables, or passes from actual to formal parameters.
Explicit castings using the casting operator?= are also possible, but not usually necessary. Using the casting operatorsCAST for an up cast in combination with inline declarations, however, can be a good way of providing a reference variable with a more general type.
See the previous example. The assignment oref = NEW sub_class( ) is an up cast, because it is followed by an reference variable of the static type of a superclass that references an object of a subclass.
If the static type of the target variable is more specific than the static type of the source variable, a check must be made at runtime (before the assignment is executed) to see whether it is less specific or the same as the dynamic type of the source variable. The name down cast arises from the fact that the movement in the inheritance space is downwards. Since the target variable can accept fewer dynamic types in comparison to the source variable, this assignment is also known as a narrowing cast. A down cast must always be performed explicitly. The following options are available:
- Using the addition INTO of the statement WHEN TYPE in a case distinction with CASE TYPE OF for object reference variables.
If this prerequisite is not met, a handleable exception is raised and the reference variable keeps its original value after the assignment.
- For non-initial source variables, the predicate expression IS INSTANCE OF or the case distinction CASE TYPE OF can be used to check whether a down cast can be applied to specific classes or interfaces.
- The dynamic type of an initial or invalid source variable is undefined. In a down cast, the null reference or an invalid reference that does not point to an object can be assigned to every target variable that can be specified here.
- When a subclass is instantiated, an associated object reference variable cannot be cast to the subclass until the instance constructors of all superclasses have been executed. When an instance constructor is executed, the dynamic type of the object reference variable is the current superclass.
The following example shows the three ways of performing a down cast for object reference variables.
- If the casting operator ?= is used, the exception CX_SY_MOVE_CAST_ERROR is caught if a down cast is not possible.
- If the casting operator CAST is used, the predicate expression IS INSTANCE OF is used in advance to check whether the down cast is possible.
- The use of the addition INTO of the statement WHEN TYPE in a case distinction with CASE TYPE OF combines the check and the down cast.
In each case, oref is assigned to the class reference variable that is reached first. Of course, a IS INSTANCE OF check would also be possible before the use of the casting operator ?=.
CLASS c1 DEFINITION.
DATA a1 TYPE string VALUE `C1`.
CLASS c2 DEFINITION INHERITING FROM c1.
DATA a2 TYPE string VALUE `C2`.
CLASS c3 DEFINITION INHERITING FROM c2.
DATA a3 TYPE string VALUE `C3`.
DATA(o) = cl_demo_output=>new( ).
DATA oref TYPE REF TO object.
oref = NEW c1( ).
cref11 TYPE REF TO c1,
cref12 TYPE REF TO c2,
cref13 TYPE REF TO c3.
cref13 ?= oref.
o->write( cref13->a3 ).
cref12 ?= oref.
o->write( cref12->a2 ).
cref11 ?= oref.
o->write( cref11->a1 ).
oref = NEW c2( ).
IF oref IS INSTANCE OF c3.
DATA(cref23) = CAST c3( oref ).
o->write( cref23->a3 ).
ELSEIF oref IS INSTANCE OF c2.
DATA(cref22) = CAST c2( oref ).
o->write( cref22->a2 ).
ELSEIF oref IS INSTANCE OF c1.
DATA(cref21) = CAST c1( oref ).
o->write( cref21->a1 ).
oref = NEW c3( ).
CASE TYPE OF oref.
WHEN TYPE c3 INTO DATA(cref33).
o->write( cref33->a3 ).
WHEN TYPE c2 INTO DATA(cref32).
o->write( cref32->a2 ).
WHEN TYPE c1 INTO DATA(cref31).
o->write( cref31->a1 ).
Assignments Between Data Reference Variables
Assignments Between Object Reference Variables