Application Development 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: 

ABAP-OO: Create any number of instances

Former Member
0 Kudos
368

Hello,

please, look at this simple scenario:


DATA:
  my_class type ref to zcl_xyz.
  create object my_class.

In that case I have just created one instance. Now I need a mechanism to create and identify any number of instance of the type zcl_xyz. <b>That means, I dont know exactly, if I need 5 or 200 instances.</b> Any feasible idea, how to handle that?

Thanks and regards.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
130

You can have an internal table which will store the reference to this object.

eg:

data:

my_class type table of ref to zcl_xyz.

do N times.

create object my_class_tmp.

append my_class_temp to my_class.

enddo.

Remember 2 reward points to replies that answered ur question.

3 REPLIES 3

Former Member
0 Kudos
131

You can have an internal table which will store the reference to this object.

eg:

data:

my_class type table of ref to zcl_xyz.

do N times.

create object my_class_tmp.

append my_class_temp to my_class.

enddo.

Remember 2 reward points to replies that answered ur question.

Former Member
0 Kudos
130

Hi,

Why don't you use internal table to store the instances of the class.

types: my_class type ref to zcl_xyz,

my_table type standard table of my_class.

Svetlin

Former Member
0 Kudos
130

Hi,

Declare one static attribute in the class.

Here is the sample code to trap the number of instance created for an object type.

CLASS C1 DEFINITION.

PUBLIC SECTION.

CLASS-DATA CREATE_COUNT TYPE I.

METHODS CONSTRUCTOR.

ENDCLASS.

DATA: O1 TYPE REF TO C1,

O2 LIKE O1,

O3 LIKE O1.

CREATE OBJECT: O1,

O2,

O3.

WRITE: 'Number of created objects:', C1=>CREATE_COUNT.

CLASS C1 IMPLEMENTATION.

METHOD CONSTRUCTOR.

CREATE_COUNT = CREATE_COUNT + 1.

ENDMETHOD.

ENDCLASS.

Hope this hint helps you.