Ansicht
Dokumentation
ABAPASSIGN_DYNAMIC - ASSIGN DYNAMIC
General Data in Customer Master Addresses (Business Address Services)This documentation is copyright by SAP AG.
ASSIGN dynamic
Dynamic ASSIGN
Variants:
4a. ASSIGN (f) TO <fs>.
4b. ASSIGN oref->(f) TO <fs>.
4c. ASSIGN (f1)=>(f2) TO <fs>.
4d. ASSIGN TABLE FIELD (f) TO
4e. ASSIGN LOCAL COPY OF MAIN TABLE FIELD (f) TO
4f. ASSIGN LOCAL COPY OF ... (f) TO <fs>.
Effect
Assigns a data object to a field symbol. When accessing reference variables, you can use the dynamic
ASSIGN to access dynamically the attributes of objects that are not known until runtime. For all variants, the return value is set as follows:
- SY-SUBRC = 0:
- Assigned successfully.
- SY-SUBRC = 4:
-
The field could not be assigned to the field symbol.
Variant 4a
ASSIGN (f) TO <fs>.
Effect
The field whose name is stored in f is assigned to the field symbol. If this field cannot be found, the field symbol remains unchanged.
Notes
You cannot use offset and length specifications ( (f)+off(len)) with a dynamic ASSIGN.
The name of the field to be assigned can also be the name of a field symbol or formal parameter (or even a component of one of these, if the field symbol or the parameter has a structure).
Example
data WA_SPFLI type SPFLI.
field-symbols
field-symbols
data F1 type STRING.
data F2 type STRING.
F1 = 'WA_SPFLI'.
F2 = '
WA_SPFLI-CARRID = 'LH'.
assign (F1) to
if SY-SUBRC = 0.
assign (F2) to
if SY-SUBRC = 0.
write
endif.
endif.
In this example, the component carrid of the structure spfli is dynamically assigned to
the field symbol
If the name of the field to be assigned is in the form (program_name)feld_name, the system searches for the field field_name in the global fields of program_name. However, it will not find it unless the program has already been loaded. (Warning: This option is for internal use by specialists only. Incompatible changes or developments may occur at any time without prior notice).
The content of the field f can be a single field name or an identifier like oref->attr.
The second option allows you to access attributes of objects whose identity is not known until runtime - such as attributes of sub-class objects, to which a superclass reference points.
The system searches for the field to be assigned in the following order:
- If the statement is in a procedure (that is, a subroutine, function module, or method), it searches first in this procedure.
- If the statement is in a method, it searches in the statically visible attributes of the associated class. In instance methods, the name is automatically prefixed with the self-reference ME-> .
- It then searches for the field in the program's global data.
- If it cannot find the field, it searches in the table work areas of the main program of the current program group. (These are declared using TABLES).
-
If the system cannot find the field, and if f contains an expression in the form oref->attr,
it searches in the attributes of the object to which oref points - that is, the dynamic type
of the reference variable is used. Note that this also applies to the self-reference ME. (ME is implicitly
inserted before the name of an attribute in instance methods, where there is no identically-named local data object).
Example
class C1 definition.
public section.
data A1(16) type C value 'A1 in Class 1'.
methods M1.
endclass.
class C1 implementation.
method M1.
field-symbols <FS> type any.
data F type STRING.
F = 'A2'.
assign (F) to <FS>.
if SY-SUBRC = 0.
write <FS>.
endif.
endmethod.
endclass.
class C2 definition inheriting from C1.
public section.
data A2(16) type C value 'A2 in Class 2'.
endclass.
data O1 type ref to C1.
start-of-selection.
create object O1 type C2.
O1->M1( ).
This example program displays 'A2 in Class
2'. A reference variable of the static type C1 points to an object of the subclass C2.
In the method M1 of the superclass C1, the field F contains the value A2.
A2 is neither the name of a local data object, nor the name ofa statically visible attribute
of the class C1. Since there is no local field A2, the system implicitly uses the expression ME->A2 and finds the dynamically addressable attribute A2 of the subclass C2.
Variant 4b
ASSIGN oref->(f) TO <fs>.
Effect
Dynamically accesses the attributes in objects. The field whose name is stored in the field f is used
as the name of the attribute when it is assigned to the field symbol. In principle, variant 4b is already
implicit in the previous variant, 4a, except that in variant 4b only the attribute name is declared dynamically, while the name of the object reference variable is declared statically.
For this reason, the same rules apply as in variant 4a, except that the search hierarchy is now simpler for those options pertaining to reference variables:
-
First, the system searches the attributes of the static type of the reference variable.
-
Then, it searches the attributes of the dynamic type of the reference variables - that is, the attributes of the class of the object to which the reference variable points.
Example
class C1 definition.
public section.
data A1(16) type C value 'A1 in Class 1'.
endclass.
class C2 definition inheriting from C1.
public section.
methods CONSTRUCTOR.
endclass.
class C2 implementation.
method CONSTRUCTOR.
A1 = 'A1 in Class 2'.
endmethod.
endclass.
field-symbols <FS> type any.
data OREF type ref to OBJECT.
data F type STRING.
start-of-selection.
F = 'A1'.
create object OREF type C1.
assign OREF->(F) to <FS>.
if SY-SUBRC = 0.
write / <FS>.
endif.
create object OREF type C2.
assign OREF->(F) to <FS>.
if SY-SUBRC = 0.
write / <FS>.
endif.
This example displays the value of the attribute A1 in the object of the class C1, then
does the same for the object of the class C2. The reference variable orefof the static
type object does not have any components of its own. However, dynamic access allows the system to access the attributes of the dynamic type.
Variant 4c
ASSIGN (f1)=>(f2) TO <fs>.
Effect
Dynamically accesses the static attributes of classes. The fields whose names are stored in the fields
f1 and f2 are used as the name of the class and of the static attribute when the assignment
to the field symbol takes place. In principle, variant 4c is already implicit in variant 4a. However, in variant 4c, the name of the class and attribute can also be individually declared statically.
Variant 4f
ASSIGN LOCAL COPY OF ... (f) TO <fs>.
Effect
Dynamic variant of the static statement: ASSIGN LOCAL COPY OF ... f TO
Exceptions
For more information on the exceptions that can be raised by the ASSIGN statement, see ASSIGN Exceptions.
Related
Additional help
Assigning Data Objects to Field Symbols.
SUBST_MERGE_LIST - merge external lists to one complete list with #if... logic for R3up CPI1466 during Backup
This documentation is copyright by SAP AG.
Length: 11197 Date: 20240419 Time: 060804 sap01-206 ( 155 ms )