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: 

ABAP OBJECTS: Dynamic Create object

Former Member
0 Kudos

Hi folks!

I have a problem... I need to create a dynamic type object with:

<b>DATA: my_instance TYPE REF TO class1.

CREATE OBJECT my_instance TYPE (class2).</b>

<i>* where class1 is a superclass of class2.</i>

When I do:

<b>my_instance ?= m_parent.</b>

<i>* where m_parent is an instance of class1</i>

My problem is when I want to access to an attribute of the class2, the compiler says that it cannot find the attribute <i>(this is OK, because the attribute is only in the class2).</i>

My question:

Is there anyway to access to an atribute of second class when is not in the first class? (i don't want to create the attribute as an attribute of the first class).

Thanx!!!!

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi David,

the object <b>my_instance</b> is an instance of a class whose name is only known at run-time. So obviously, the compiler will not know any attributes of the class. and hence the syntax error.

I suggest that you re-think about the logic of your program. If the class can only be specified at run time, how can its attributes be specified at compile-time ? I hope you're able to get what I'm trying to convey....

Regards,

Anand Mandalika.

3 REPLIES 3

Former Member
0 Kudos

Hi David,

the object <b>my_instance</b> is an instance of a class whose name is only known at run-time. So obviously, the compiler will not know any attributes of the class. and hence the syntax error.

I suggest that you re-think about the logic of your program. If the class can only be specified at run time, how can its attributes be specified at compile-time ? I hope you're able to get what I'm trying to convey....

Regards,

Anand Mandalika.

0 Kudos

Hi Poornanand:

The problem is when I do:

<b>my_instance ?= m_parent.</b>

in the debugger I can see all the attributes of my_instance (the inherit attributes of the class1 and the its). I can acces to the inherit attributes (because type of my instance is the superclass).

My question is if is there anyway to access to non inherit attributes? (field-symbols, references, and so on)

(if it is no way I will have to re-think my program)

0 Kudos

Hi David,

When you do the debugging, you are dealing with run-time - i.e., the program is now running and you are just interrupting it at each statement to examine the program state. You will reach the point where the object is already created. That is why you can see all those attributes. But when you comiple, the program is not yet <i>running</i>, so the attributes will be unknown because of the dynamic type specification.

I think you will have to redesign the program logic. As i had already said in my earlier post, it is not proper to have the attributes specified statically while the class itself is specified dynamically.

Your situation is somewhat similar to -

DATA ITAB TYPE TABLE OF SPFLI.

PERFORM TEST TABLES ITAB.

FORM TEST TABLES ITAB.
  LOOP AT ITAB.
    WRITE: / ITAB-CARRID.
  ENDLOOP.
ENDFORM.

Hope the point is clear.

Regards,

Anand Mandalika.