Wednesday, September 21, 2016

Data Reference Variable

1. What is Data Reference Variable?

Data Reference Variables are similar to pointer in other languages like C. It is the data object that holds the address of other data object. So when we access the data reference variable, we are accessing the address of another data object. In order to access the contents of data object which is pointed by reference variable, we must use the technique called dereferencing.

2. Declaring the Data Reference Variable

A data reference variable is declared using the type ref to keyword. We can specify a fully defined type or a generic type:

data: lr_a TYPE REF TO i.

Here we have declared a data reference variable lr_a by using the complete type i. Which means lr_a can from now hold the address of any other data object of type integer. However, we will not be able to declare a data reference variable with an incomplete type like c or n (Incomplete type means, those data types which requires a length specification).

 3. Accessing the data object using the data reference variable.

Now, since we are ready with the reference variable, we will try assigning a data object to it.

data: 
lr_a type ref to i,
lv_a type i value 90.

get reference of lv_a into lr_a.
write: lr_a->*

Output: 90

We need to use the keyword get reference of in order to assign  the address of any data object to the data reference variable. After this process, for accessing the actual content of the data object using the data reference variable, we use the dereferencing operator ( ->* ). 

4. Creating the data object explicitly by using the reference variable

So far we have assigned the address of an existing data object to the data reference variable. Now, we will see another variation of the data object declaration by using a generic type. The only two supported generic type are data and object. A data reference variable is declared by using the type data can point to any data object where as the ones which are declared by using the object can point to objects of classes.

datalr_data type REF TO data.
CREATE DATAlr_data type LENGTH 8.

Here we have declared a generic data reference variable. And we used the create data statement to create it implicitly. This statement will create a data object in  the memory and assign the address of the data object to the data reference variable lr_data. The original name of the data object that was created by this statement is not known and it is of no interest. In this method, we will be able to use the incomplete type like c and n with the length specification as well. However will not be able to give a default value by adding the value keyword. We can also use the like keyword to refer to an object already existing. The memory for the data object is not assigned till the create data statement.   

No comments:

Post a Comment

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...