‎2007 May 24 8:11 AM
Hallo Buddies,
I have a scenario where I have to create the object of class dynamically,
The scenario is like this:
User inputs the class name, and I have to create the object of the user input class dynamically.
Here, I know the interface of the user input class, and hence can get the reference to interface which I can point to the object created dynamically from the user input class.
How can I do it? A code snippet will do a lot of help.
Thanks and Regards,
Ankit
‎2007 May 24 8:19 AM
parameter p_cls(30)
data obj type ref to <interface>.
CREATE OBJECT obj TYPE (p_cls).
Reward with points, if useful.
Darshil
‎2007 May 24 8:17 AM
Hi Ankit,
Check this sample... and change according to your requirement.
DATA: BEGIN OF obj_sales_order
sales_order_no LIKE <sales_order_no_type>,
<...>,
objref TYPE REF TO <SALES_ORDER_CLASS>
END OF obj_sales_order,
reftab LIKE SORTED TABLES OF obj_sales_order
WITH UNIQUE KEY sales_order_no
<other key attributes>.
Now all you need to do is to do a SELECT...ENDSELECT loop, and:
SELECT ...
obj_sales_order-sales_order_no = <selection result>
obj_sales_order-<other key attr> = <selection result>
READ TABLE reftab INTO obj_sales_order
FROM obj_sales_order.
IF sy-subrc <> 0.
CREATE OBJECT obj_sales_order-objref
EXPORTING ...
EXCEPTIONS ...
IF sy-subrc <> 0.
MESSAGE ...
CONTINUE.
ELSE.
INSERT obj_sales_order INTO TABLE reftab.
ENDIF.
ENDIF.
CALL METHOD obj_sales_order-objref-><method>.
ENDSELECT.
Regards,
Raj
‎2007 May 24 8:19 AM
parameter p_cls(30)
data obj type ref to <interface>.
CREATE OBJECT obj TYPE (p_cls).
Reward with points, if useful.
Darshil
‎2007 May 24 8:32 AM