ā2015 Feb 06 9:51 AM
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
ā2015 Feb 06 10:36 AM
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 ?
ā2015 Feb 06 10:41 AM
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
ā2015 Feb 06 11:24 AM
lref_data1 ?= lref_data.
Instead of the above, call the add method in lref_data1, and pass it the same reference variable.
ā2015 Feb 06 11:35 AM
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.
ā2015 Feb 06 11:58 AM
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.
ā2015 Feb 06 12:46 PM
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.
ā2015 Feb 06 1:09 PM
ā2015 Feb 06 1:26 PM
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.
ā2015 Feb 06 1:33 PM
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