Mentor SAP

Data References

Data references and object references were introduced alongside field symbols as part of the enhancements in SAP R/3 4.6A. From this point on, ABAP features full “reference semantics”.

 

Data reference variables contain data references or pointers to data objects.

 

Use the TYPE REF TO addition for the TYPES statement to define a reference type for a data object. You can specify the data type explicitly or choose the generic variation by using TYPE REF TO DATA . In this case, your data reference can point to any type of data object.

 

The corresponding DATA statement defines the data reference variable. Reference variables are data objects that contain the address of any other data object of the specified type.

 

Data references use reference semantics; that is, when a data reference variable is accessed, the data reference is addressed. This means any changes to the data references affect the addresses.

 

Data reference variables are handled in ABAP like other data objects with an elementary data type. This means a reference variable can be defined not only as a single field, but also as the smallest indivisible unit of complex data objects such as structures or internal tables.

 

After a reference variable has been defined, it is INITIAL (empty), and thus contains a blank pointer. For a data reference variable to contain a reference that points to a data object, use the statement GET REFERENCE OF to obtain a reference to a data object that has already been defined.

 

You can also assign an existing data reference from another data reference variable or create a data object dynamically with the reference.

 

 

Addressing the Content of Referenced Data Objects

Use the dereferencing operator ->* to dereference statically-typed data objects directly. This means that the dereferencing operator directly accesses the content of the data object that the reference is pointing to. For compatibility reasons, you must dereference generically typed data references (TYPE REF TO data) and assign them to a field symbol, which you then use to access the content.

 

 

Filling a Reference with Expression REF

 

The figure gives you an example. With statement GET REFERENCE a helper variable is filled and then passed to the import parameter of the method.

 

The new REF operator condenses this coding into a single expression.

 

Note: The pound sign is used to indicate that the type of the data reference should be derived from the context, which, in this case, is the definition of the method parameter. The syntax check ensures that the data object in the parentheses is type-compatible with the requested reference type.

 

Field Symbols

Before the introduction of data references, field symbols were used in ABAP as dereferenced pointers. Field symbols provide symbolic access to an existing data object. All accesses you make to the field symbol are made to the data object assigned to that field symbol. Therefore, you can only access the content of the data object to which the field symbol points. This technique is referred to as the value semantics of field symbols.

 

You declare field symbols using the FIELD-SYMBOLS statement. Note that the angle brackets (<>) in the field symbol name are part of the syntax. Use the ASSIGN statement to assign a data object to the field symbol.

 

By specifying a type for the field symbol, you can ensure that only compatible objects are assigned to the field symbol.

 

 

Access to Internal Tables Using Data References and Field Symbols

Data references and field symbols provide new access options when combined with internal tables.

 

With the statements described in figure you can define field symbols and data references, which you then can use to point to individual lines of an internal table. If such a field symbol or data reference points to a line of the internal table you can access this table line via the field symbol or by dereferencing the data reference.

 

Hint: Field symbols and references point to the assigned row even after the internal table is re-sorted.