There are two types of reference variable:ĭata reference variable can store the reference to any data object (variable, structures, internal tables etc.) whereas Object reference variable can store the reference to any class object.įor data reference variables, either the generic data type or a completely specified data type can be specified. Now this field symbol can be used to do any valid operation on the internal table.ĭifference between data reference and object reference: After that we have dereferenced the data reference lr_tab into a generic field symbol. In CREATE DATA statement, the type of data object is mentioned in parenthesis which means that the type will be determined at runtime based on the value of parameter p_tname. Here lr_tab is a generic data reference and is a generic field symbol for internal table. SELECT * FROM (p_tname) INTO TABLE UP TO 10 ROWS. Solution: PARAMETERS: p_tname TYPE tabname.ĬREATE DATA lr_tab TYPE TABLE OF (p_tname). Requirement: Selection screen parameter “ Table Name” will take a table name as input and display the corresponding table entries as output. Now, to access individual component of MARA structure, ASSIGN COMPONENT statement can be used. Here CREATE DATA statement creates an anonymous data object (MARA structure) and assigns its reference to the generic data reference lr_str, which then can be dereferenced into a generic field symbol.
Working with structures: DATA: lr_str TYPE REF TO data.ĪSSIGN COMPONENT 'MATNR' OF STRUCTURE TO. After ASSIGN statement you should check sy-subrc If field symbol assignment is successful, sy-subrc will be 0 otherwise it will be 4.So in case of generic data reference, it can only be dereferenced using a field symbol, and this field symbol can be used at any operand position to manipulate the value of data object as shown below: DATA: lr_num TYPE REF TO data.
Hence the below statement would not be allowed. Now since lr_num is generic, lr_num->* cannot be directly used at operand position. And second statement creates an anonymous data object of type “i” and assigns its reference to lr_num. Here first statement declares a generic data reference lr_num which can point to any data object. Generic data reference can be declared as: DATA: lr_num TYPE REF TO data. SELECT * FROM mara INTO TABLE lt_mara UP TO 10 ROWS.
While processing internal table row, we can use REFERENCE INTO statement to set references to table rows as shown below: DATA: lr_mara TYPE REF TO mara. Here individual components of the structure can be accessed with -> operator on data reference variable. Working with structures: DATA: lr_mara TYPE REF TO mara. With ABAP 7.40, instead of GET REFERENCE, the REF operator also can be used to assign the reference of an existing data object to a data reference.Here lv_num is an existing data object (not anonymous data object). If you want to assign the reference of an existing data object to a data reference, you can use GET REFERENCE statement. With ABAP 7.40, instead of CREATE DATA, the NEW operator can also be used to create an anonymous data object and assigns its reference to a data reference variable.Īssigning existing data object to data reference:.Now if we want to change the value of data object, then it can be done by dereferencing lr_num by using dereference operator ->* as shown below: DATA lr_num TYPE REF TO i. And second statement creates an anonymous data object of type “i” and assigns the reference of this data object to lr_num. Here first statement declares a reference variable lr_num which can point to any data object of type “i”. Typed data reference variable can be declared as: DATA lr_num TYPE REF TO i. There can be two types of data references: Data reference cannot be used at operand position directly it should be dereferenced first. But to access the actual data object which data reference is pointing to, we first need to deference it using dereferencing operator ->*.ĭifference between field symbol and data reference:įield symbol is a placeholder for data object to which it is assigned and points to the content of data object hence it can be used at any operand position (no need to dereference it) and works with the content of the referenced memory area (value semantics).ĭata references are pointers to data objects and it contains the memory address of data object (reference semantics). It stores the memory address of any data object. So data references are nothing but pointers. In this blog I am going to explain about data references and its significance in dynamic programming.Īccording to SAP documentation, Data references can point to any data objects or to their parts (components, rows of internal tables, or sections specified by offsets and lengths). In my last blog I explained about field symbols, below is the link for same: