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

Calling method of object whose type is not known until runtime

Former Member
0 Likes
452

Hiya,

In SE80 class builder I have attribute R_OBJECT TYPE REF TO OBJECT.

In my CONSTRUCTOR method, I do a:

CREATE OBJECT R_OBJECT TYPE (lc_variable)

,where lc_variable contains type I want to use.

This seems to compile, but when I try to call a method using the following statement, the compiler complains saying that the method is not known. Well, of course it's not known, I haven't told it which class the object belongs to yet!

CALL METHOD R_OBJECT->MY_METHOD RECEIVING lc_result.

What am I doing wrong?

Thanks,

Tristan

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
408

Hello Tristan

The <b>static</b> type of r_object is OBJECT and, therefore, will never have any methods you have defined. In order to call your method you must do <b>narrow casting</b>, e.g:

DATA:
  lo_myclass     TYPE REF TO <lc_variable>.  " your class


CREATE OBJECT R_OBJECT TYPE (lc_variable).

* Narrow casting required
  lo_myclass ?= r_object.

  lc_result = lo_myclass->my_method( ).  " now it works

Regards

Uwe

Hiya,

In SE80 class builder I have attribute R_OBJECT TYPE REF TO OBJECT.

In my CONSTRUCTOR method, I do a:

CREATE OBJECT R_OBJECT TYPE (lc_variable)

,where lc_variable contains type I want to use.

This seems to compile, but when I try to call a method using the following statement, the compiler complains saying that the method is not known. Well, of course it's not known, I haven't told it which class the object belongs to yet!

CALL METHOD R_OBJECT->MY_METHOD RECEIVING lc_result.

What am I doing wrong?

Thanks,

Tristan

1 REPLY 1
Read only

uwe_schieferstein
Active Contributor
0 Likes
409

Hello Tristan

The <b>static</b> type of r_object is OBJECT and, therefore, will never have any methods you have defined. In order to call your method you must do <b>narrow casting</b>, e.g:

DATA:
  lo_myclass     TYPE REF TO <lc_variable>.  " your class


CREATE OBJECT R_OBJECT TYPE (lc_variable).

* Narrow casting required
  lo_myclass ?= r_object.

  lc_result = lo_myclass->my_method( ).  " now it works

Regards

Uwe