Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Assignments →  Assigning References →  Setting Field Symbols →  ASSIGN →  ASSIGN - mem_area 

ASSIGN - dynamic_access

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... { cref->(attr_name) } 
  | { iref->(attr_name) }
  | { (clif_name)=>(attr_name) }
  | { (clif_name)=>attr }
  | { clif=>(attr_name) } ...

Alternatives

1. ... cref->(attr_name)

2. ... iref->(attr_name)

3. ... (clif_name)=>(attr_name)

4. ... (clif_name)=>attr

5. ... clif=>(attr_name)

Effect

These alternatives to specifying the memory area mem_area of the statement ASSIGN dynamically are designed especially for dynamic access to attributes of classes or interfaces (Dynamic Access).

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; sy-subrc needs to be checked as well.


Note

Field symbols to which instance attributes (or parts of instance attributes) are assigned have a memory-preserving affect on the associated object.

Alternative 1

... cref->(attr_name)

Effect

This form may be used for all visible attributes of objects. cref can be any class reference variable pointing to an object which contains the attribute specified in a character-like field attr_name. The system searches for the attribute first in the static type of cref and then in the dynamic type.

The attribute name does not need to be specified in uppercase letters. It can contain offsets/lengths, structure component selectors, object component selectors, and class component selectors, in order to assign parts of the attribute (or referenced objects of the attribute).


Example

Dynamic assignment of an object attribute to a field symbol. The assignment is made via an object reference variable of the static type of the root class object, which can reference any object.

CLASS demo DEFINITION. 
  PUBLIC SECTION. 
    METHODS meth IMPORTING oref TYPE REF TO object 
                           attr TYPE string. 
ENDCLASS. 

CLASS demo IMPLEMENTATION. 
  METHOD meth. 
    ASSIGN oref->(attr) TO FIELD-SYMBOL(<attr>). 
    ... 
  ENDMETHOD. 
ENDCLASS. 

Alternative 2

... iref->(attr_name)

Effect

This form may be used for all visible attributes of objects. iref can be any interface reference variable pointing to an object which contains the interface attribute specified in a character-like field attr_name. The search for this method takes place only in the static type of iref.

The attribute name does not need to be specified in uppercase letters. It can contain offsets/lengths, structure component selectors, object component selectors, and class component selectors, in order to assign parts of the attribute (or referenced objects of the attribute).


Example

Dynamic assignment of an object attribute to a field symbol. The assignment is made via an interface reference variable.

INTERFACE intf. 
  CONSTANTS attr TYPE string VALUE `interface attribute`. 
ENDINTERFACE. 

CLASS demo DEFINITION. 
  PUBLIC SECTION. 
    INTERFACES intf. 
ENDCLASS. 

DATA iref TYPE REF TO intf. 
iref = NEW demo( ). 

ASSIGN iref->('attr') TO FIELD-SYMBOL(<fs>). 

cl_demo_output=>display( <fs> ). 

Alternative 3

... (clif_name)=>(attr_name)

Alternative 4

... (clif_name)=>attr

Effect

These forms may be used for all visible static attributes. Classes and interfaces and also attributes can be specified dynamically in character-like fields clif_name or attr_name. The attribute attr can, however, also be specified directly. A search is made for the class or interface first and then the attribute.

The content of attr_name and clif_name does not need to be specified in uppercase letters. attr_name can contain offsets/lengths, structure component selectors, object component selectors, and class component selectors, in order to assign parts of the attribute (or referenced objects of the attribute). Here, no offsets/lengths or object component selectors can be specified after a directly specified attribute attr.


Note

If, in clif_name, a class of another program is specified using an absolute type name, this program is loaded into a new additional program group or into the current program group, depending on the program type (if not already loaded). If required, the program constructor is also executed.

Alternative 5

... clif=>(attr_name)

Effect

This form may be used for all visible static attributes. The class or interfaces is specified directly and the attribute specified dynamically in a character-like field attr_name. A search is made for the attribute in the specified class or interface.

The content of attr_name does not need to be specified in uppercase letters. attr_name can contain offsets/lengths, structure component selectors, object component selectors, and class component selectors, in order to assign parts of the attribute (or referenced objects of the attribute).


Example

Various dynamic assignments of a static class attribute to field symbols.

DATA(clif_name) = `cl_abap_browser`. 
DATA(attr_name) =  `xlarge`. 

ASSIGN (clif_name)=>(attr_name) TO FIELD-SYMBOL(<fs1>). 
ASSIGN (clif_name)=>xlarge TO FIELD-SYMBOL(<fs2>). 
ASSIGN cl_abap_browser=>(attr_name) TO FIELD-SYMBOL(<fs3>). 

cl_demo_output=>display( 
  |(clif_name)=>(attr_name):    { <fs1> }\n| && 
  |(clif_name)=>xlarge:         { <fs2> }\n| && 
  |cl_abap_browser=>(attr_name): { <fs3> }\n| ).