‎2012 Jan 26 2:23 PM
Hi experts,
I have an internal table that contains several different instances of a class. Now I'm creating a new instance which has the exact same contents as one instance in my internal table. Is it possible to check if I have the same content in one instance in the internal table as in my new instance with a check like READ TABLE itab with key NODE = NODE ? With the READ command it doesn't work because of different instances.
Ln Node (Reference)
1 ->
2 ->
3 ->
4 ->
5 ->
6 ->
7 ->
8 ->
9 ->
Kind regards,
Alex
‎2012 Jan 26 2:46 PM
Comparing the entire object will success only when there internal representational numbers are same. i.e
Since you have different objects, You can try to compare the attributes if they are not many.
CLASS lcl_data DEFINITION.
PUBLIC SECTION.
DATA: v_num TYPE i.
ENDCLASS. "lcl_data DEFINITION
DATA: io_data TYPE STANDARD TABLE OF REF TO lcl_data.
DATA: o_data TYPE REF TO lcl_data.
DO 5 TIMES.
CREATE OBJECT o_data.
o_data->v_num = sy-index.
APPEND o_data TO io_data.
ENDDO.
CREATE OBJECT o_data.
o_data->v_num = 3.
READ TABLE io_data TRANSPORTING NO FIELDS WITH KEY table_line->v_num = o_data->v_num.
if sy-subrc eq 0.
write: 'Found'.
else.
WRITE: 'No Success'.
endif.
What do you want to do after you find the object - Use the existing object instead of the new one? If yes, you should consider implementing [Singleton Design Pattern|http://help-abap.zevolving.com/2008/09/abap-object-design-patterns-singleton/] based on the Key.
Regards,
Naimesh Patel
‎2012 Jan 26 2:40 PM
‎2012 Jan 26 2:46 PM
Comparing the entire object will success only when there internal representational numbers are same. i.e
Since you have different objects, You can try to compare the attributes if they are not many.
CLASS lcl_data DEFINITION.
PUBLIC SECTION.
DATA: v_num TYPE i.
ENDCLASS. "lcl_data DEFINITION
DATA: io_data TYPE STANDARD TABLE OF REF TO lcl_data.
DATA: o_data TYPE REF TO lcl_data.
DO 5 TIMES.
CREATE OBJECT o_data.
o_data->v_num = sy-index.
APPEND o_data TO io_data.
ENDDO.
CREATE OBJECT o_data.
o_data->v_num = 3.
READ TABLE io_data TRANSPORTING NO FIELDS WITH KEY table_line->v_num = o_data->v_num.
if sy-subrc eq 0.
write: 'Found'.
else.
WRITE: 'No Success'.
endif.
What do you want to do after you find the object - Use the existing object instead of the new one? If yes, you should consider implementing [Singleton Design Pattern|http://help-abap.zevolving.com/2008/09/abap-object-design-patterns-singleton/] based on the Key.
Regards,
Naimesh Patel
‎2012 Jan 26 4:11 PM
The comparison of the class objects was exactly what did help me with my problem. Thanks