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: 

How to declare a class object which will be created dynamically.

bikash_bansal
Product and Topic Expert
Product and Topic Expert
0 Kudos
179

Hi,

The following code snippet does not work. How can i declare the variable lo_content so that

i can achieve the object creation based on class availability.

********************************************************************************

DATA:

lo_content TYPE REF TO data .

if class 'cl_ap_a2x_service_manager' exists.(using a FM SEO_CLASS_GET)

CREATE OBJECT lo_content TYPE ('cl_ap_a2x_service_manager').

endif.

********************************************************************************

I cannot declare it as: lo_content TYPE REF TO cl_ap_a2x_service_manager .

I will not allow to activate the code as class cl_ap_a2x_service_manager doesnot exist in the same system.

Regards,

Bikash.

1 ACCEPTED SOLUTION

MarcinPciak
Active Contributor
0 Kudos
100

You are mixing data references (type ref to DATA) with object references (type ref to OBJECT). The latter should be used in this case (so that any object type can be stored in this reference, not any data ).


DATA:
lo_content TYPE REF TO object .

CREATE OBJECT lo_content TYPE ('CL_AP_A2X_SERVICE_MANAGER').

Regards

Marcin

4 REPLIES 4

SuhaSaha
Advisor
Advisor
0 Kudos
100

Check this:

DATA:
lo_content TYPE REF TO data .

if class 'cl_ap_a2x_service_manager' exists.(using a FM SEO_CLASS_GET)
CREATE DATA lo_content TYPE REF TO ('CL_ABAP_STRUCTDESCR').
endif.

MarcinPciak
Active Contributor
0 Kudos
101

You are mixing data references (type ref to DATA) with object references (type ref to OBJECT). The latter should be used in this case (so that any object type can be stored in this reference, not any data ).


DATA:
lo_content TYPE REF TO object .

CREATE OBJECT lo_content TYPE ('CL_AP_A2X_SERVICE_MANAGER').

Regards

Marcin

0 Kudos
100

>

> You are mixing data references (type ref to DATA) with object references (type ref to OBJECT). The latter should be used in this case (so that any object type can be stored in this reference, not any data )

Hey Marcin,

I was about to correct my post Anyway today i had my first experience with generic object reference variable (OBJECT) !!

DATA:
lo_content TYPE REF TO object,
it_bdc_msg TYPE ZLMT_LINE.

FIELD-SYMBOLS: <attr> TYPE ANY.

CREATE OBJECT lo_content TYPE ('ZCL_CALL_TRANSACTION')
EXPORTING IM_TRANSACTION = 'SE11'.

CALL METHOD lo_content->('GET_BDC_MESSAGES')
RECEIVING RE_BDC_MESSAGES = it_bdc_msg.

Cheers,

Suhas

0 Kudos
100

No worries Suhas

One thing I would like to point at. I think these two statements are syntatically incorrect

CREATE OBJECT lo_content TYPE ('ZCL_CALL_TRANSACTION')

EXPORTING IM_TRANSACTION = 'SE11'.

CALL METHOD lo_content->('GET_BDC_MESSAGES')

RECEIVING RE_BDC_MESSAGES = it_bdc_msg.

This is because you are providing the class/method name dynamically, but its interface statically. Syntax checker will not recognize this signature as a valid one, unless it knows the name of class/method it is reffering to - as this comes during runtime, the interface parameters must also be provided dynamically.

Cheers

Marcin

PS: I just spotted in one of my programs that I was wrong with the above statement. Interface can be statical even class/method is provided dynamically. It is similar to calling FM where we deliver its name dynamically but interface is static. Sorry for this confusion

Edited by: Marcin Pciak on Sep 27, 2010 1:00 PM