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

Appending Objects in an internal Table

Former Member
0 Likes
2,158

Hallo Guru,

I'm looking for a way to append newly created objects of a class "XYZ" in an internal table.

Best Regards,

Kais

1 ACCEPTED SOLUTION
Read only

rainer_hbenthal
Active Contributor
0 Likes
1,326

Declare a field in the table with


    oref           type ref to ZCL_MY_CLASS,

and store your object in it.

7 REPLIES 7
Read only

Former Member
0 Likes
1,326

Hi,

Can you elaborate your query in details?

Regards,

Ankur Parab

Read only

0 Likes
1,326

Hi Ankur,

I've created a new class.

And now I created a Progam which 2 or 3 instances of this class has been created, using CREATE OBJECT.

I put some information on it.

Now, I want to use an internal table, which ref to my class.

And I want to save every instance with its information in the internal table.

So, later I'm going to loop over the internal table to print out the information.

Regards,

Kais

Read only

rainer_hbenthal
Active Contributor
0 Likes
1,327

Declare a field in the table with


    oref           type ref to ZCL_MY_CLASS,

and store your object in it.

Read only

0 Likes
1,326

Hi Rainer,

Thanks for your Respond.

What I actually looking for is how to insert the object in the table.

This's how it looks like.



DATA : it_employees TYPE TABLE OF REF TO lcl_employee WITH HEADER LINE,
             r_employee TYPE REF TO lcl_employee.

START-OF-SELECTION.

  CREATE OBJECT r_employee
    EXPORTING
      im_name        = '******'
      im_firstname   = '*****'
      im_base_salary = 1000
      im_dob         = '******'.

  r_employee->set_address( im_address = '*******' ).

*???? How to insert the Object in The table

  LOOP AT it_employees.
    it_employees->print( ).
  ENDLOOP.

Regards,

Kais

Read only

0 Likes
1,326

Hi

U need to append the object in the table like a normal workarea:

CLASS LC_MY_CLASS DEFINITION.
  PUBLIC SECTION.
    METHODS: CONSTRUCTOR
              IMPORTING P_NAME TYPE CHAR20,
             WRITE_NAME.
  PRIVATE SECTION.
    DATA: NAME TYPE CHAR20.
ENDCLASS.                    "LC_MY_CLASS DEFINITION

CLASS LC_MY_CLASS IMPLEMENTATION.
  METHOD CONSTRUCTOR.
    NAME = P_NAME.
  ENDMETHOD.                    "CONSTRUCTOR

  METHOD WRITE_NAME.
    WRITE: / NAME.
  ENDMETHOD.                    "WRITE_NAME
ENDCLASS.                    "LC_MY_CLASS IMPLEMENTATION

TYPES: TY_OBJ TYPE REF TO LC_MY_CLASS.

DATA: MY_OBJ TYPE REF TO LC_MY_CLASS,
      T_OBJ  TYPE TABLE OF TY_OBJ.

START-OF-SELECTION.

  CREATE OBJECT MY_OBJ
    EXPORTING
      P_NAME = 'MAX'.

  APPEND MY_OBJ TO T_OBJ.

  CREATE OBJECT MY_OBJ
    EXPORTING
      P_NAME = 'MAX 2'.

  APPEND MY_OBJ TO T_OBJ.

  LOOP AT T_OBJ INTO MY_OBJ.
    CALL METHOD MY_OBJ->WRITE_NAME.
  ENDLOOP.

Max

Read only

0 Likes
1,326

Thanks Max.

I has solved the Problem.

Read only

uwe_schieferstein
Active Contributor
0 Likes
1,326

Hello Kais

If you already on ECC 6.0 then you may have a look at the iterator class SWF_UTL_OBJECT_TAB. An appropriate table type for you would be SWF_UTL_OBJECT_TAB.

Regards

Uwe