Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Assignments →  Setting References →  ASSIGN →  ASSIGN - mem_area 

ASSIGN - dynamic_access

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... { cref->(attr_name) } 
  | { iref->(attr_name) }
  | { (class_name)=>(attr_name) }
  | { (class_name)=>attr }
  | { class=>(attr_name) } ... .

Alternatives

1. ... cref->(attr_name)

2. ... iref->(attr_name)

3. ... (class_name)=>(attr_name)

4. ... (class_name)=>attr

5. ... class=>(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 (Dynamic Access).

In these variants, the ASSIGN statement 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 logical 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).

Alternative 2

... iref->(attr_name)

Effect

This form may be used for all visible interface attributes of objects. iref can be any interface reference variable pointing to an object which contains the interface attributes 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).

Alternative 3

... (class_name)=>(attr_name)

Alternative 4

... (class_name)=>attr

Alternative 5

... class=>(attr_name)

Effect

These forms may be used for all visible static attributes. You can specify both the class and the attribute dynamically. It is also possible to specify the attribute attr and the class class directly.

If you use the alternatives with dynamic class specification (class_name), the system first searches for the class and then for the attribute. If you use the static specification class, the system searches for the attribute in the existing class.

The content of attr_name and class_name does not have to be in uppercase letters. attr_name can contain offsets/lengths, structure component selectors, object component selctors, and class component selectors, to assign parts of attributes or referenced objects of attributes. If you specify the class name dynamically and the attribute directly, you cannot specify any offsets/lengths or object component selectors for the attribute behind the attribute.


Note

If, in class_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.


Example

Dynamic access to an attribute of an object using a field symbol.

CLASS c1 DEFINITION. 
  PUBLIC SECTION. 
    METHODS m1 IMPORTING oref TYPE REF TO object 
                         attr TYPE string. 
ENDCLASS. 

CLASS c1 IMPLEMENTATION. 
  METHOD m1. 
    FIELD-SYMBOLS <attr> TYPE ANY. 
    ASSIGN oref->(attr) TO <attr>. 
    WRITE <attr> ... 
  ENDMETHOD. 
ENDCLASS.