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: 

Dynamic Object Instances

Former Member
0 Kudos

In my program I want to create an unknown number of object instances at runtime from an existing class. Is there a way to dynamicallly create object instances during runtime and have access to each individual object's instance methods. I would also like to store the instances in a table of objects, but can't find the exact syntax for creating an itab or structure of object instances. I have searched for this in the forums and online, but can't seem to get anything that actually gives me exactly what I am looking for (google syndrome).

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

You can explicitly give the type while instantiating the object. If you runtime class is inherited from the super class, you can the Super class to define the variable. This way, you would get the access to all the methods / attributes at compile time which are there in the ZCL_SUPER.


data: lo_obj type ref to zcl_super.
data: lv_class type string.
lv_Class = 'ZCL_SUPER_KID'.
create object lo_obj type (lv_class).

If any of the component doesn't exist in the defined class, you need to call the method dynamically. If method is not there is the object, it would result in the runtime error.


   call method lo_obj->('METH_NOT_IN_SUPER').

To store them in the table, you can do something like this:


data: LT_OBJ type standard table of ref to zcl_super.
data:  lo_obj type ref to ZCL_SUPER.
data: lv_class type string.
lv_Class = 'ZCL_SUPER_KID'.
create object lo_obj type (lv_class).
append lo_obj to LT_OBJ.

OR


types: begin of lty_obj,
             obj type ref to ZCL_SUPER,
           end   of lty_obj.
data: LT_OBJ type standard table of lty_obj.
data: lwa_obj like line of LT_OBJ.

data: lv_class type string.
lv_Class = 'ZCL_SUPER_KID'.
create object lwa_obj-obj type (lv_class).
append lwa_obj to LT_OBJ.

Regrads,

Naimesh Patel

6 REPLIES 6

naimesh_patel
Active Contributor
0 Kudos

You can explicitly give the type while instantiating the object. If you runtime class is inherited from the super class, you can the Super class to define the variable. This way, you would get the access to all the methods / attributes at compile time which are there in the ZCL_SUPER.


data: lo_obj type ref to zcl_super.
data: lv_class type string.
lv_Class = 'ZCL_SUPER_KID'.
create object lo_obj type (lv_class).

If any of the component doesn't exist in the defined class, you need to call the method dynamically. If method is not there is the object, it would result in the runtime error.


   call method lo_obj->('METH_NOT_IN_SUPER').

To store them in the table, you can do something like this:


data: LT_OBJ type standard table of ref to zcl_super.
data:  lo_obj type ref to ZCL_SUPER.
data: lv_class type string.
lv_Class = 'ZCL_SUPER_KID'.
create object lo_obj type (lv_class).
append lo_obj to LT_OBJ.

OR


types: begin of lty_obj,
             obj type ref to ZCL_SUPER,
           end   of lty_obj.
data: LT_OBJ type standard table of lty_obj.
data: lwa_obj like line of LT_OBJ.

data: lv_class type string.
lv_Class = 'ZCL_SUPER_KID'.
create object lwa_obj-obj type (lv_class).
append lwa_obj to LT_OBJ.

Regrads,

Naimesh Patel

0 Kudos

Thanks for your help. Just to be clear, what you are suggesting is that I use the super class to define the type of subclass object? I think this is close to what I need, but not quite. I know which class I need to create instances of, but not the number of instances of that class. So at runtime the program will create objects of the known class, but how many is determined based on runtime needs. My problem is in creating unique instances. For example...

DATA objectname TYPE REF TO classname.

LOOP AT ITAB.

CREATE OBJECT objectname.

ENDLOOP.

Depending on the size of the tabel at runtime, the number of entries is how many class instances I would need, but how would I name them dynamically to be unique instances or create them to be unique so I can reference a specific instance later in the program. Is your suggestion going in that direction or not quite the same result?

0 Kudos

I guess, you are looking for something like this:


*
CLASS lcl_cal DEFINITION.
  PUBLIC SECTION.
    DATA: v_indicator TYPE i READ-ONLY.
    METHODS: constructor IMPORTING iv_ind TYPE i.
    METHODS: do_calc.
ENDCLASS.                    "lcl_cal DEFINITION
*
CLASS lcl_cal IMPLEMENTATION.
  METHOD constructor.
    v_indicator = iv_ind.
  ENDMETHOD.                    "constructor
  METHOD do_calc.
    WRITE: / v_indicator, 'called'.
  ENDMETHOD.                    "do_calc
ENDCLASS.                    "lcl_cal IMPLEMENTATION
*
START-OF-SELECTION.
  DATA: lt_calcs TYPE STANDARD TABLE OF REF TO lcl_cal.
  DATA: lo_calc TYPE REF TO lcl_cal.
  DO 10 TIMES.   " << Number of objects
    CREATE OBJECT lo_calc
      EXPORTING
        iv_ind = sy-index.
    APPEND lo_calc TO lt_calcs.
  ENDDO.
"  Processing each object
  LOOP AT lt_calcs INTO lo_calc.
    lo_calc->do_calc( ).
  ENDLOOP.

Regards,

Naimesh Patel

0 Kudos

Halo Zmwhite,

As Naimesh suggested you can use sy-tabix to uniquely identify the instances that you created out of ITAB. But I dont think that is good OOPS Principle.

Your itab entries should contain a column say 'NAME' or 'ID' that uniquely identifies an entry . So you can use that ID to create the unique instance.


data: l_object type ref to <class_name>,
         lt_objects type table of ref to <class_name>.
loop at itab into wa.
create object l_object exporting i_id = wa_id.
append l_object to lt_object.
endloop.

then you can use read statements o lt_objects

.

Regards

Arshad

0 Kudos

I agree, with

you can use sy-tabix to uniquely identify the instances that you created out of ITAB. But I dont think that is good OOPS Principle.

The code lines are just for reference, it is not for copy-&-paste. It should be molded as per the actual requirement.

Regards,

Naimesh Patel

0 Kudos

Thanks for your help guys, I wasn't sure if it would be possible to create multiple instances of the same object with a non class attribute to identify it.