ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Assignments → Assigning References → Setting Field Symbols → ASSIGN → ASSIGN - mem_area
ASSIGN - dynamic_dobj
Other versions: 7.31 | 7.40 | 7.54
Syntax
... { (name) }
| { dref->* }
| { dobj INCREMENT inc }
| { COMPONENT comp OF STRUCTURE struc } ...
Alternatives
2. ... dref->*
3. ... dobj INCREMENT inc
4. ... COMPONENT comp OF STRUCTURE struc
Effect
These alternatives to specifying the memory area
mem_area of the statement ASSIGN dynamically are used to dynamically access data objects.
In an inline declaration of the field symbol using
FIELD-SYMBOL(<fs>), its typing is performed with the generic type data.
In these variants, the statement ASSIGN sets the return code sy-subrc.
If the assignment is successful, sy-subrc is set to 0; if not, it is set to 4. If the assignment is not successful, the field symbol keeps its previous state. It is therefore not enough just to evaluate the
predicate expression
<fs> IS ASSIGNED in a dynamic ASSIGN; sy-subrc needs to be checked as well.
Alternative 1
... (name)
Effect
In this dynamic variant of mem_area, the memory area is not specified directly,
but as content of a character-like data object (name) in parentheses. The following can be specified for name:
- Literal or constants
name is specified as a character literal or as a constant, it can be evaluated statically and the specified data object is identified as the used object.
- Variable
name is specified as a variable, it is specified only dynamically and the content is not evaluated statically.
When the statement is executed, name is not evaluated until runtime (in both
cases). The name in name is structured in the same way as if specified directly:
When executing the statement, the content of name must be the name of a data
object which may contain offsets and lengths, structure component selectors, and component selectors
for assigning structured data objects and attributes in classes or objects. The content of name does not have to be in uppercase letters.
name can contain a chain of names consisting of component selectors. For an individual name or if the first name is followed by an object component selector
(->), the specified data object is searched for according to the following hierarchy:
me (special case of cref->(attr_name) in
dynamic_access) is scanned. TABLES are scanned. me (special case of cref->(attr_name) in
dynamic_access) is scanned.
If the data object is found and the name is followed by an object component selector
(->), the search for the following names is continued from left to right,
as described under dynamic_access.
If the first name is followed by a class component selector
(=>), the specified class is searched for, as described under
dynamic_access, and the search is then continued accordingly from left to right.
Notes
-
Dynamically specifying a structure component using a structure component selector produces worse performance than using the addition
COMPONENT OF STRUCTURE(see this executable example). -
If an attribute of a class in a different program is specified in
nameusing an absolute type name and followed by the class component selector (=>), it is loaded into a new additional program group or into the current program group (if not already loaded), depending on the program type. Any existing program constructors are not executed, however, unlike in a genuinedynamic_access. -
For internal use only, the name in
namecan also have the form "(PROG)DOBJ", where "PROG" is the name of an ABAP program and "DOBJ" the name of a global data object of this program (these names are not case-sensitive). If the program "PROG" is loaded into the same internal session as the current program when the statementASSIGNis executed, the data object "DOBJ" is found in this program and the field symbol points to this data object if the assignment was successful. -
In an obsolete variant, the addition TABLE
FIELD can be specified before
name. This restricts the search to table work areas.
Example
Dynamic output of the content of any system field. The validity of the input is checked before it is
dynamically assigned with (name) to field symbol syfield via an application of classes of
RTTI on structure SYST
DATA(name) = `sy-uzeit`.
cl_demo_input=>request( EXPORTING text = `System field`
CHANGING field = name ).
name = to_upper( name ).
DATA(components) = CAST cl_abap_structdescr(
cl_abap_typedescr=>describe_by_name( 'SYST' ) )->components.
IF NOT line_exists( components[ name = replace( val = name
sub = `SY-`
with = `` ) ] ).
cl_demo_output=>display( `Unknown system field` ).
RETURN.
ENDIF.
ASSIGN (name) TO FIELD-SYMBOL(<syfield>).
IF sy-subrc = 0.
cl_demo_output=>display( |{ name }: { <syfield> }| ).
ENDIF.
Alternative 2
... dref->*
Effect
When specifying a dereferenced data reference dref for mem_area
using the dereferencing operator ->*, the memory area of the data object
is assigned to the field symbol to which dref points. If the reference variable
dref does not reference a data object, the assignment is not performed and sy-subrc is set to 4.
Unlike all other operand positions for which the data reference dref must
be fully typed for dereferencing, dref can be typed generically in the statement
ASSIGN using TYPE REF TO data. Dereferencing of
a data reference that does not point to a data object also raises an non-handleable exception in all cases except in the statement ASSIGN.
Example
Creates a local copy of a global data object g_dat in a procedure using a data reference dref and a local field symbol <l_dat>.
DATA g_dat TYPE string VALUE '...'.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS meth.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD meth.
DATA dref TYPE REF TO data.
FIELD-SYMBOLS <l_dat> TYPE any.
CREATE DATA dref LIKE g_dat.
ASSIGN dref->* TO <l_dat>.
<l_dat> = g_dat.
cl_demo_output=>display_data( <l_dat> ).
ENDMETHOD.
ENDCLASS.
Alternative 3
... dobj INCREMENT inc
Effect
This expression for mem_area assigns a memory area to the field symbol that
has the same length as the memory area of dobj and is incremented inc
times this length in reference to the memory area of dobj. inc
expects a numeric data object. A data object or a field symbol must be specified directly for
dobj. Offset or length specifications or the dereferencing of a data reference are not possible.
The field symbol cannot be declared via an inline declaration FIELD-SYMBOL(<fs>).
Notes
The dynamic ASSIGN variant with INCREMENT is designed
for sequential access to similar memory areas that are located at regular intervals after each other,
such as consecutive structure components of the same data type. In all other cases, ASSIGN ... INCREMENT should be used carefully. Note the following in particular:
-
Usually the addition
RANGEmust be used to defined the area, within which it is possible to work withINCREMENT. -
The assigned memory area is handled using the data type
dobjif the additionCASTINGis not specified in casting_spec. This means that an implicit casting of the assigned memory areas to the data type ofdobjis performed. -
The typing check also refers to
dobj, but is performed only when the statement is executed. -
Runtime errors always occur if the following general rule is violated: If
deep data objects that are in the assigned memory area do not match the typing as far as type and position are concerned.
Example
After the ASSIGN statement, the field symbol points to the fourth component
col4. See the example for the addition RANGE.
DATA:
BEGIN OF struct,
col1 TYPE string VALUE `COL1`,
col2 TYPE string VALUE `COL2`,
col3 TYPE string VALUE `COL3`,
col4 TYPE string VALUE `COL4`,
col5 TYPE string VALUE `COL5`,
END OF struct.
FIELD-SYMBOLS <fs> TYPE string.
ASSIGN struct-col1 INCREMENT 3 TO <fs> RANGE struct.
cl_demo_output=>display( <fs> ).
Executable Example
Field Symbols, ASSIGN INCREMENT
Alternative 4
... COMPONENT comp OF STRUCTURE struc
Effect
This expression for mem_area assigns a memory area of a component comp of a structure struc to the field symbol.
struc is a result position. The structure can be specified as a data object or as a
writable expression.
If struc is specified as an expression, its result must be structured. If struc is specified as a data object, the result does not need to be structured.
comp is a character-like or
numeric expression position. The evaluation depends on the data type of comp:
-
If the field
comphas a text-like type (corstring) or the type of a flat structure, which exclusively contains character-like components, the field content is interpreted as the name of the component. The name must be in uppercase letters. It may contain offsets and lengths, structure component selectors, and component selectors for assigning structured data objects and attributes in classes or objects. -
If the field
comphas a non-text-like elementary type, the content is converted to the typeiand interpreted as the position of the component in the structure. If the value ofcompis 0, the memory area of the entire structure is assigned to the field symbol. -
If
comphas a different type, a syntax error or runtime error occurs.
If an operand struc specified as a data object is not a structure or the
specified component is not found, the assignment is not made and sy-subrc is set to 4.
Notes
-
Identifying a component by its name is far more performance-intensive than using its position, since
far more internal processes are involved. Using
COMPONENTS OF, however, always produces better performance than specifying the name after the structure component selector within a fully dynamically specified component in a parenthesized data objectname(see this executable example). -
If the structure
strucis specified as a table expression and the corresponding row is not found, the exception CX_SY_ITAB_LINE_NOT_FOUND is raised. -
Writable expressions can be specified for
strucbut no other expressions, since these can have a non-temporary result. Assigning a component of a temporary structure to a field symbol would not make sense. -
If
strucis specified, it is advisable to specify only structures as a data object and to check this in advance. Just evaluatingsy-subrcis not enough to determine why an assignment was not successful.
Example
Assignment of all components of a structure to a field symbol in a loop. In every loop pass, the component is assigned whose position is determined by the loop index.
SELECT SINGLE *
FROM scarr
WHERE carrid = 'UA'
INTO @DATA(wa).
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE wa TO FIELD-SYMBOL(<fs>).
IF sy-subrc <> 0.
EXIT.
ENDIF.
cl_demo_output=>write( <fs> ).
ENDDO.
cl_demo_output=>display( ).
Example
The following two methods show the dynamic assignment of the components of a structure (passed to the parameter para of the methods) to a field symbol <comp>.
-
The first implementation does not use
RTTI. The statement
DESCRIBE FIELDis used to check whether the passed data object is a structure. The components are then assigned one after the other to the field symbol in aDOloop. -
The second implementation uses RTTI. A
down cast of the type description
object to the class CL_ABAP_STRUCTDESCR for the passed data object ensures
that the object is a structure. A loop across the component table COMPONENTS assigns the components to the field symbol via their names.
CLASS demo DEFINITION.
PUBLIC SECTION.
METHODS: meth1 IMPORTING para TYPE data,
meth2 IMPORTING para TYPE data.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD meth1.
DESCRIBE FIELD para TYPE DATA(dtype).
IF dtype <> 'u' AND dtype <> 'v'.
RETURN.
ENDIF.
DO.
ASSIGN COMPONENT sy-index
OF STRUCTURE para TO FIELD-SYMBOL(<comp>).
IF sy-subrc <> 0.
EXIT.
ENDIF.
...
ENDDO.
ENDMETHOD.
METHOD meth2.
TRY.
DATA(struct_descr) = CAST cl_abap_structdescr(
cl_abap_typedescr=>describe_by_data( para ) ).
CATCH cx_sy_move_cast_error.
RETURN.
ENDTRY.
LOOP AT struct_descr->components
ASSIGNING FIELD-SYMBOL(<comp_descr>).
ASSIGN COMPONENT <comp_descr>-name
OF STRUCTURE para TO FIELD-SYMBOL(<comp>).
...
ENDLOOP.
ENDMETHOD.
ENDCLASS.
Example
Assigns a component of a row of an internal table to a field symbol.
TYPES:
BEGIN OF struc,
col1 TYPE i,
col2 TYPE i,
END OF struc.
DATA itab TYPE STANDARD TABLE OF struc WITH EMPTY KEY.
itab = VALUE #( ( col1 = 1 col2 = 2 )
( col1 = 3 col2 = 4 ) ).
ASSIGN COMPONENT 2 OF STRUCTURE itab[ 2 ] TO FIELD-SYMBOL(<fs>).
cl_demo_output=>display( <fs> ).