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

Check internal table for class instance with same content

0 Likes
1,435

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

1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
821

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

3 REPLIES 3
Read only

huseyindereli
Active Contributor
0 Likes
821

Hi ,

We have made a discussion about your topic. It should be what you're asking.

Take a look at [ THIS THREAD|; .

Regards.

Read only

naimesh_patel
Active Contributor
822

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

Read only

0 Likes
821

The comparison of the class objects was exactly what did help me with my problem. Thanks