4

I have an internal table filled with the type reference for the objects I want to create, like so (first line is the header of internal table ITAB):

+==========+===============+
| OBJ_TYPE | OBJ_CLASS_REF |
+==========+===============+
| TYPE1    | ZCL_CLASS1    |
|----------|---------------|
| TYPE2    | ZCL_CLASS2    |
+----------+---------------+

What I would like to do in my program is this (I put line numbers):

1   LOOP AT itab
2    "Concatenate LO_ and the value of ITAB-OBJ_TYPE
3     CONCATENATE 'LO_' itab-obj_type INTO v_obj_name.
4    "Create a reference object
5     CREATE DATA (v_obj_name) TYPE REF TO itab-obj_type.
6     CREATE OBJECT (v_obj_name).
7   ENDLOOP

How do I successfully do lines 5 and 6?

2
  • Greetings, what do you want to do with those objects ? Do they have common methods or properties you want to use ? Commented Mar 13, 2012 at 16:40
  • @tomdemuyt For now, they don't have common methods or properties. I'm sort of trying to create a similar functionality to SCII wherein you create a new class using templates CL_CI_CATEGORY and CL_CI_SCAN for when you want to add additional checks to SCI. What I thought of doing was to create a custom z-table to hold all values for the optional tests the user may want to do. Thus dynamically creating the objects at runtime because it's not till then that the program know what checks the user wishes to execute. I don't know if my design is good so please advise. Commented Mar 13, 2012 at 23:59

1 Answer 1

8

First of all, it's a good idea to provide an interface or an abstract superclass and have your various classes implement that interface or subclass that abstract class - this will save you a lot of unnecessary casting. So let's say you have ZIF_FOO with classes ZCL_BAR and ZCL_BAZ implementing it. The table could be

TYPES: BEGIN OF t_line
         type_name TYPE seoclass,
         instance  TYPE REF TO zif_foo,
       END OF t_line.
DATA: lt_instances TYPE STANDARD TABLE OF t_line,
      ls_instance  TYPE t_line.

Then you can fill the table like this:

ls_instance-type_name = 'ZCL_BAR'. " or wherever you get this value from
CREATE OBJECT ls_instance-instance TYPE (ls_instance-type_name).

If you want to use local classes, you can do the same - just use a longer type name (SEOCLASS with its 30 characters won't be enough) and specify the type name as described in the online documentation of the RTTI:

ls_instance-typename = '\PROGRAM=ZMYREPORT\CLASS=LCL_MYCLASS'.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.