Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Need help for retaining the reference variable data

Former Member
0 Likes
4,070

Hi,

I have a problem in clearing the class reference. I defined a class with a reference variable as an attribute in the class(type ref to data).

I am some how filling the reference variable with some data in the class method.

Now in my report I am processing the following code:

Data: lref_data   Type Ref To zcl_test.

Data: lref_data1 Type Ref To zcl_test.

Create Object lref_data.

Create Object lref_data1.

call Method lref_data->add.  (In this method I am filling the reference variable with some data).

lref_data1 ?= lref_data.

Free lref_data->mref_data.

So here when I am clearing the reference variable in lref_data automatically my reference variable in lref_data1 is getting cleared.

I know that since they are both pointing to same reference If one is get cleared other will automatically cleared.

But I want lref_data1->mref_data not to get cleared.

I thought of passing that reference value first in a local variable and after clearing again I can assign it. But I think it is not a good approach to follow.

Please anybody help me with this issue.

Thanks

9 REPLIES 9
Read only

Former Member
0 Likes
2,391

Hi Chitti babu,

    

                   

after you passed the iref_data to iref_data1.

why can't you clear iref_data

FREE iref_data.

why you want to  clear Free lref_data->mref_data alone ?

Read only

0 Likes
2,391

Hi Surendra Prasad,

Thanks for the solution. If I clear the lref_data it works fine. But my requirement is like I need to clear only the reference attribute in the object. So I am using Free lref_data->mref_data. Even if I clear the lref_data, I want the copied data to stay as it is.

Thanks.

Chitti Babu

Read only

Former Member
0 Likes
2,391

lref_data1 ?= lref_data.

Instead of the above, call the add method in lref_data1, and pass it the same reference variable.

Read only

0 Likes
2,391

Hi Eric,

Thanks for your answer. Your idea can work. But according to my requirement the class has lot of attributes and some of them are reference attributes. I am filling them in different places. So according to you I need to write the same code twice I think.

Read only

0 Likes
2,391

Yes, to maintain 2 seperate references to the same class, you need 2 sets of code.  Instead have 1 reference and append each new instance to an internal table.  Then you need only 1 set of code.  

Read only

0 Likes
2,391

Hi Eric,

Can you please elaborate your answer how can we append instance to an internal table. I dont have any idea how to append it.

Thanks.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,391
  1. Why are you copying the reference from one variable to another?
  2. What is the business logic you are trying to realise through your code?
Read only

Former Member
0 Likes
2,391

Hi Suhas,

     According to our logic, we need some backup of all the transactional data. So before  navigating from one record to other we ll be copying the data from reference to other. After copying the data to some local reference variable, in order to fill new data we are clearing the first reference object. My issue is coming there itself.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,391

So before  navigating from one record to other we ll be copying the data from reference to other.

This, IMHO, doesn't seem like OO programming. It seems like you want to marry procedural & OO programming techniques

Anyway every "object" should have a different instance & you can collect these instances in a collection (e.g., IF_OBJECT_COLLECTION). You can then use the collection later for processing.

BR,

Suhas