Data Reference
Data Reference
Data References are pointers to Data Objects. They occur in ABAP as the contents of Data
Reference Variable.
We can Create New Data Objects Dynamically with The help of data references.
But We can Only DeReference a Data Reference using a Special Allignment To field
Symbol Only.
Reference Varibale:
Reference variables contains only references Actual values of reference variable are not
visible they are hidden.
Syntax:
We can create a data reference variable either by referring to the above data type or using:
Reference variables are initial when you declare them. They do not point to an object. We
cannot Dereference an initial reference variable.
To create a data object dynamically during a program we need a data reference variable
<dref>.
Syntax:
This statement creates a data object in the internal session of the current ABAP program.
the data reference in the data reference variable <dref> points to the object.
We can Even Specify the Data Type Of Data object dynamically using
Syntax:
Here, <name> is the name of a field that contains the name of the required data type.
Syntax:
If the data reference in <dref> is initial or invalid, we cannot dereference it. The field symbol
remains unchanged, and SY-SUBRC is set to 4.
Syntax:
<obj> can be a statically-declared data object, or a field symbol pointing to any object
(including a dynamic object).
Example Program:
REPORT zbr_datareference.
DATA: BEGIN OF student,
name TYPE c LENGTH 6 VALUE 'SACHIN',
rno TYPE i VALUE 10,
END OF student.
DATA dref TYPE REF TO data.
*TYPES dref type ref to data.
CREATE DATA dref LIKE student.
FIELD-SYMBOLS <fs> STRUCTURE student DEFAULT %DUMMY%.
ASSIGN dref->* TO <fs> CASTING.
IF <fs> IS ASSIGNED.
<fs>-name = 'SACHIN'.
<fs>-rno = 21.
WRITE: / <fs>-name, <fs>-rno.
ELSE.
SKIP.
ENDIF.
FIELD-SYMBOLS <fs1> TYPE i.
DATA dref2 TYPE REF TO data.
GET REFERENCE OF <fs>-rno INTO dref2.
ASSIGN dref2->* TO <fs1> CASTING.
IF <fs1> IS ASSIGNED.
WRITE: <fs1>.
ELSE.
EXIT.
ENDIF.
OUTPUT:
--------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------
SACHIN 21 21