Dynamic Programming – Reference Variables

Reference variables are defined using TYPE REF TO keyword in DATA statement. Reference variable points to the object which exist at run time. Read this blog to find out how to create variables at runtime and how is it different from variables defined using DATA statement.

Let’s start with the concept which is familiar.

To use ALV Grid we first define a reference variable of type class CL_GUI_ALV_GRID. Then at later stage during program execution we create object using CREATE OBJECT statement.

DATA : grid TYPE REF TO cl_gui_alv_grid.

CREATE OBJECT grid
  EXPORTING
    i_parent = custom_container.

When program is loaded in memory there isn’t any object of class CL_GUI_ALV GRID that exist until statement CREATE OBJECT is executed. With CREATE OBJECT statement object is created and its reference is assigned to grid reference variable. This is actually dynamically creating object at runtime and this is different from how normally we define variables using DATA statement. For example if you have defined internal table using DATA statement then this internal table is created and available for you to access in LOAD-OF-PROGRAM or INITIALIZATION event.

With this concept in mind lets proceed and see how this is applied to creating variables at runtime. Consider variables as class definition. I am not saying they are but it would be easier if we assume for a moment to understand. I can create an integer variable at runtime using following statements.

DATA : ref_i TYPE REF TO i .
CREATE DATA ref_i .

This is very similar to way we create objects. This different than defining a variable directly using DATA: vari_i TYPE i. In case of reference variable, variable does not exist until CREATE DATA is executed.

Below example will make more sense. Here I have defined one reference variable and one normal variable. You can access normal variables once program start executing but reference variable can only be accessed once they are created using CREATE DATA statement.

DATA : ref_i TYPE REF TO i .
DATA : vari_i TYPE       i .

*--------------------------------------------------------------------*
* Load of Program
*--------------------------------------------------------------------*
LOAD-OF-PROGRAM .
* Can not access REF_I yet
  vari_i = '1' .

*--------------------------------------------------------------------*
* Start of Selection
*--------------------------------------------------------------------*
START-OF-SELECTION .
* Can not access REF_I yet
  vari_i = '2' .

  CREATE DATA ref_i .
* Now you can access REF_I
  ref_i->* = '21' .
  WRITE : ref_i->* .

You would have noticed value of variable is accessed differently. This is called dereferencing data reference. To access contents of data object you need to dereference the data first. Proper way of doing this is using field symbol.

DATA :          ref_i  TYPE REF TO i .
FIELD-SYMBOLS : <fs_i> TYPE        i .

*Create variable
CREATE DATA ref_i .

*Dereference
ASSIGN ref_i->* TO <fs_i> .

*Access
<fs_i> = 1 .
WRITE : <fs_i>.

You might ask why so much trouble to define integer variable and you are right for defining one integer variable it is not worth but think about scenario where you don’t know what kind of variable you need whether it’s going to be predefined defined, based on data element, a structure, a structure with unknown fields, an internal table. In such scenario reference variable comes to rescue.

This program sums it all. User inputs table name in input parameter, program create dynamic internal table based on input, selected data from table and displays it.

REPORT  zpw_dyn.

DATA : ref_tab TYPE REF TO data.     "Generic definition
FIELD-SYMBOLS : <fs_table> TYPE ANY TABLE ,
                <fs_workarea> TYPE ANY ,
                <fs_field> TYPE ANY .

PARAMETERS : p_tab TYPE char20 .

CREATE DATA ref_tab TYPE STANDARD TABLE OF (p_tab) .
ASSIGN ref_tab->* TO  <fs_table> .

SELECT *
  INTO TABLE <fs_table>
  UP TO 10 ROWS
 FROM (p_tab) .

LOOP AT <fs_table> ASSIGNING <fs_workarea> .

  DO.
    ASSIGN COMPONENT sy-index OF STRUCTURE <fs_workarea> TO <fs_field> .
    IF sy-subrc = 0 .
      WRITE : <fs_field>.
    ELSE.
      EXIT.
    ENDIF.
  ENDDO.
  NEW-LINE .
ENDLOOP.

2 Replies to “Dynamic Programming – Reference Variables

Leave a Reply