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

using a generic object

Former Member
0 Likes
1,502

I would like to be able to create an instance of an ABAP object without knowing the object type first. for example, if a specific type of message hits a queue, create a handler object specific to that message. I need to be able to declare the object variable as something not strongly typed, and do the create object for whatever actual object i want to use. I have checked the docs, but found nothing.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,078

Hi,

Take a look at the document https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/documents/a1-8-4/advanced and generic programming in abap.pdf and the webcast titled <b>Advanced and Generic Programming in ABAP</b> in the SAP TechEd Education Sessions available on SDN. It will help you in getting an idea about runtime type specification. The book Abap Objects gives a detailed description of the concepts as well.

If your prgram is using inheritance/interfaces, you can use polymorphism to achieve your purpose. Otherwise, take a look at the documentation for <b>CREATE DATA dref TYPE REF TO type|(typename)</b> and cl_abap_refdesc=>create_by_name( 'CLASS_NAME' )

Hope this helps.

Regards

Message was edited by: Shehryar Khan

5 REPLIES 5
Read only

Former Member
0 Likes
1,078

Hello David,

If I have understood your requirement correctly, then I think it is possible to meet it. Consider the following example :



data w_obj type ref to object.
data meth(5) type c value 'WRITE'.

class test definition.
  public section.
    data:
      lv_test_variable type c value 'A'.
    methods write.
endclass.

class test implementation.
  method write.
    write:
        lv_test_variable,
        'Simple Sample'.
  endmethod.
endclass.

start-of-selection.

create object w_obj type test.

call method w_obj->(meth).

As you are sure to discover, this approach has some limitations. I have not studied this thoroughly, but at least you can begin thinking in the lines of creating Object References.

I hope this has been of some help to you.

Regards,

Anand Mandalika.

Read only

Former Member
0 Likes
1,079

Hi,

Take a look at the document https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/documents/a1-8-4/advanced and generic programming in abap.pdf and the webcast titled <b>Advanced and Generic Programming in ABAP</b> in the SAP TechEd Education Sessions available on SDN. It will help you in getting an idea about runtime type specification. The book Abap Objects gives a detailed description of the concepts as well.

If your prgram is using inheritance/interfaces, you can use polymorphism to achieve your purpose. Otherwise, take a look at the documentation for <b>CREATE DATA dref TYPE REF TO type|(typename)</b> and cl_abap_refdesc=>create_by_name( 'CLASS_NAME' )

Hope this helps.

Regards

Message was edited by: Shehryar Khan

Read only

0 Likes
1,078

Hi David,

I think this feature can be achieved only in WAS 6.40. ( Someone correct me here, if I am wrong ).

This syntax is available, perhaps only in 6.40 WAS, and didn't work in 4.6C.


CREATE DATA dref TYPE HANDLE <class_name>.

Since, I do not have 6.40 WAS, I will give a shot-in-the-dark solution:


data: lv_variable(25) type c value 'CL_GUI_ALV_GRID'.
data: myref type ref to data.

field-symbols: <FS> type any.

assign lv_variable to <fs>.
if sy-subrc = 0.
  create data myref type handle <fs>.
endif.

I do not guarantee this will work, but with the document link, which Shehryar Khan has given in the previous post, I am guessing this should work.

If it works, then you can create an object at runtime, of any classname.

Regards,

Subramanian V.

Read only

0 Likes
1,078

Hi,

The statements <b>DATA <dref> TYPE REF TO DATA</b> and <b>CREATE DATA <dref> TYPE <type>|LIKE <obj></b> are available since ver 4.6. So creating a data/object reference dynamically and calling its methods should not be an issue even in versions earlier than 6.40. The HANDLE addition perhaps requires a newer version.

Regards

Read only

0 Likes
1,078

Thanks Shehryar. I was aware of that. I was only concerned about the 'HANDLE' clause.

Regards,

Subramanian V.