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

Dynamic ref to objects

Former Member
0 Likes
553

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

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
528

parameter p_cls(30)

data obj type ref to <interface>.

CREATE OBJECT obj TYPE (p_cls).

Reward with points, if useful.

Darshil

3 REPLIES 3
Read only

Former Member
0 Likes
528

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

Read only

Former Member
0 Likes
529

parameter p_cls(30)

data obj type ref to <interface>.

CREATE OBJECT obj TYPE (p_cls).

Reward with points, if useful.

Darshil

Read only

0 Likes
528

Hi Darshil,

Thank you, question answered.

Regards

Ankit