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

access components of object referenced by o_ref of static_type OBJECT w/h dynamic_type of cl_class

rajivsap
Explorer
0 Kudos
1,781

How to access attributes and methods of object referenced by object_reference of static_type OBJECT created with a dynamic_type of cl_class where cl_class its own attributes and methods.

CLASS cl_name DEFINITION. 
PUBLIC SECTION. 
DATA title TYPE string. 

METHODS: "NO CONSTRUCTOR 
change_title IMPORTING i_title TYPE string , 
display_title.
ENDCLASS.
CLASS cl_name IMPLEMENTATION. 
METHOD change_title. 
title = i_title. 
ENDMETHOD. 
METHOD display_title.
DATA: lv_string TYPE string.
 CONCATENATE 'Title of the class is' lv_string INTO lv_string SEPARATED BY space. 
MESSAGE lv_string TYPE 'I'. 
RETURN. 
ENDMETHOD.
ENDCLASS.


START-OF-SELECTION. 
data l_rcl_object type ref to object. 
CREATE OBJECT l_rcl_object TYPE cl_name. 

"HOW TO ACCESS title of the object referenced by l_rcl_object->title or call method l_rcl_object->change_title or"l_rcl_object->display_title?
1 ACCEPTED SOLUTION
Read only

maheshpalavalli
Active Contributor
1,681

it would be a better approach if you can create an interface with the attributes and methods that you will be reusing in the sub classes. Then you can create an object of type that interface and instantiate with that class reference. Then you can use that interface to access the common attributes and objects.

Else if you can dynamically acces like shown in the below help

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abenclass_components_addressing.htm

8 REPLIES 8
Read only

maheshpalavalli
Active Contributor
1,682

it would be a better approach if you can create an interface with the attributes and methods that you will be reusing in the sub classes. Then you can create an object of type that interface and instantiate with that class reference. Then you can use that interface to access the common attributes and objects.

Else if you can dynamically acces like shown in the below help

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abenclass_components_addressing.htm

Read only

0 Kudos
1,681

Thanks a lot. Dynamic access solved my query and found a better approach.

Read only

Sandra_Rossi
Active Contributor
1,681

The compiler/syntax checker sees that TITLE is not a member of OBJECT "class" so that gives an error. You have to use what is called "down casting":

DATA(l_rcl_name) = CAST cl_name( l_rcl_object ).
OR
DATA l_rcl_name TYPE REF TO cl_name.
l_rcl_name ?= object.

then you can use L_RCL_NAME->TITLE, etc.

Read the ABAP documentation for more information.

Read only

0 Kudos
1,681

Thank you.

Read only

touzik_itc
Active Participant
1,681

As alternative to explicit cast, proposed by Sandra, the syntax for dynamic call could be used:

CALL METHOD l_rcl_object->('CHANGE_TITLE') EXPORTING i_title = 'Some title'.
CALL METHOD l_rcl_object->('DISPLAY_TITLE').

additionally title variable is missing in the method display_title

CONCATENATE 'Title of the class is' title INTO lv_string SEPARATED BY space.
Read only

0 Kudos
1,681

Thanks a lot. This solved my query.

Read only

Sandra_Rossi
Active Contributor
1,681

Please edit your question, select your code and press the "CODE" button to make it correctly colorized/indented, so that it's easier for us to analyze it. Thank you.

Read only

rajivsap
Explorer
0 Kudos
1,681

Done and thanks once again.