‎2008 Nov 02 1:22 PM
Hello Experts,
I am trying to understand this casting.
This is the hierachy:
cl_abap_typedescr
cl_abap_datadescr
cl_abap_elemdescr
cl_abap_complexdescr
cl_abap_structdescr
cl_abap_tabledescr
cl_abap_refdescr
cl_abap_objectdescr
cl_abap_classdescr
cl_abap_intfdescrDATA: r_descr TYPE REF TO cl_abap_typedescr,
r_descrstructdescr TYPE REF TO cl_abap_structdescr,
r_descrstructdescr ?= r_descr .
LOOP AT r_descrstructdescr->components ASSIGNING <comp_wa>.
.......
ENDLOOP.Why is this casting with r_descrstructdescr ?= r_descr necessary.
r_descrstructdescr was cl_abap_structdescr and after the casting it is now
cl_abap_typedescr.
Originally r_descrstructdescr was also able to use this function r_descrstructdescr->components.
Is it correctly ?
After this assignment r_descrstructdescr ?= r_descr it is now no longer able
to use all the functions of the other classes like l_abap_elemdescr, cl_abap_complexdescr
s.o. because it has became cl_abap_typedescr now.
The classes at the bottom have more functions than the classes
it sabove in the above shown hierarchy. What sence does it make to restrict "r_descrstructdescr"
due to this casting r_descrstructdescr ?= r_descr . ?
Or do I misunderstand it totally wrong
Regards
sas
‎2008 Nov 03 9:16 AM
hi,
if I understand it correctly then r_descr is being casted to cl_abap_structdescr which was privously cl_abap_typedescr through this
assignment.
r_descrstructdescr ?= r_descr
If so then I have understood it.
Regard
sas
‎2008 Nov 02 2:33 PM
cl_abap_typedescr has no attribute "components". Since your actual value of cl_abap_typedescr is the inheriting object cl_abap_structdescr, you have to cast in order to access the attribute.
Regards,
Thomas
‎2008 Nov 02 4:32 PM
‎2008 Nov 02 7:23 PM
Hello,
can you say me what happens due to this operation r_descrstructdescr ?= r_descr .
Do I cast cl_abap_typedescr to cl_abap_structdescr or
cl_abap_structdescr to cl_abap_typedescr ????????DATA: r_descr TYPE REF TO cl_abap_typedescr,
r_descrstructdescr TYPE REF TO cl_abap_structdescr,
r_descrstructdescr ?= r_descr .
bye
sas
‎2008 Nov 02 8:04 PM
>
> Hello,
>
> can you say me what happens due to this operation r_descrstructdescr ?= r_descr .
>
>
Do I cast cl_abap_typedescr to cl_abap_structdescr or
> cl_abap_structdescr to cl_abap_typedescr ????????>
> DATA: r_descr TYPE REF TO cl_abap_typedescr,
> r_descrstructdescr TYPE REF TO cl_abap_structdescr,
>
> r_descrstructdescr ?= r_descr .
>
> bye
> sas
Thomas has given you a great answer. It's absolutely precise and accurate.
cl_abap_typedescr has no attribute "components". Since your actual value of cl_abap_typedescr is the inheriting object cl_abap_structdescr, you have to cast in order to access the attribute.
You cast the subclass to the super class. The object referred to by r_descr is in fact a cl_abap_structdescr object - it does have "components". But so long as the reference variable is defined with reference to cl_abap_typedescr, you can't get at them. It's like the functionality is there, but the button on your remote control doesn't exist. By ?= to a reference type that does have "components" you can access them.
This is quite a difficult concept to get hold of. It is generic to object oriented programming, so you may like to tackle it through an language that's a bit easier - like Java.
matt
‎2008 Nov 02 8:05 PM
‎2008 Nov 02 8:09 PM
Hello Erdem
Perhaps sample report ZUS_SDN_ABAPOO_STATIC_VS_DYN may help you to understand casting.
*&---------------------------------------------------------------------*
*& Report ZUS_SDN_ABAPOO_STATIC_VS_DYN
*&
*&---------------------------------------------------------------------*
*& Thread: casting operation
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1110425"></a>
*&---------------------------------------------------------------------*
REPORT zus_sdn_abapoo_static_vs_dyn.
TYPE-POOLS: abap.
DATA: go_typedescr TYPE REF TO cl_abap_typedescr,
go_strucdesr TYPE REF TO cl_abap_structdescr.
DATA: gs_knb1 TYPE knb1.
DATA: gs_component TYPE abap_compdescr.
START-OF-SELECTION.
go_typedescr = cl_abap_typedescr=>describe_by_data( gs_knb1 ).
" NOTE: static ref type = CL_ABAP_TYPEDESCR
" dynamic ref type = CL_ABAP_STRUCTDESCR
" Syntax error: attribute COMPONENTS unknown
** LOOP AT go_typedescr->components INTO gs_component.
** ENDLOOP.
go_strucdesr ?= go_typedescr.
" NOTE: static ref type = dynamic ref type = CL_ABAP_STRUCTDESCR
LOOP AT go_strucdesr->components INTO gs_component.
ENDLOOP.
BREAK-POINT.
go_complexdescr ?= go_typedescr.
" NOTE: static ref type = CL_ABAP_COMPLEXDESCR
" dynamic ref type = CL_ABAP_STRUCTDESCR <<< check in debugger
" Thus, nothing changed...
** go_classdescr ?= go_typedescr. => Dump
BREAK-POINT.
END-OF-SELECTION.
Regards
Uwe
‎2008 Nov 02 9:21 PM
Dear Friends it is not clearly for me yet.
We need a CL_ABAP_STRUCTDESCR in order to access to the attribute "components",
but we are casting through this assignment cl_abap_structdescr to cl_abap_typedescr.
r_descrstructdescr ?= r_descr .
How can we ever access to "CL_ABAP_STRUCTDESCR's" components attribute if we cast the subclass CL_ABAP_STRUCTDESCR to the super class "CL_ABAP_TYPEDESCR"
Please tell me is "r_descrstructdescr " equal CL_ABAP_STRUCTDESCR or
is "r_descrstructdescr " equal CL_ABAP_TYPEDESCR
after this aasignment r_descrstructdescr ?= r_descr .
Regards
sas
‎2008 Nov 02 11:09 PM
Hi Erdem,
actually, the cast described in your statement gives to r_descrstructdescr the reference to CL_ABAP_STRUCTDESCR.
TypeDescr is the root object of the RTTI hierarchy, and lets you to "describe" any kind of object in the DDIC.
But, in order to work with the described object, you need to downcast it to the specialized kind.
In fact, a structure IS a type and the system describes it to you giving the opportunity to handle it as a "Type" (CL_ABAP_TYPEDESCR) and as a "Structure" (CL_ABAP_STRUCTDESCR).
So, you ask the system to describe a type and it replies to you saying:
"Ok, it's a type but in more depth it's a structure".
Afterwards, you can work with the general type (TypeDescr) or with the specialized one (StructDescr) by casting it.
Hope this helps,
Roby.
‎2008 Nov 02 11:22 PM
Please tell me is "r_descrstructdescr " equal CL_ABAP_STRUCTDESCR or
is "r_descrstructdescr " equal CL_ABAP_TYPEDESCR
after this aasignment r_descrstructdescr ?= r_descr .
bye
erdem
‎2008 Nov 02 11:51 PM
the cast described in your statement gives to r_descrstructdescr the reference to CL_ABAP_STRUCTDESCR
It's equal to CL_ABAP_STRUCTDESCR.
The static type declaration doesn't change.
You have declared r_descrstructdescr as TYPE REF CL_ABAP_STRUCTDESCR.
Bye,
R.
‎2008 Nov 03 8:48 AM
hi,
can you confirm 100 % percent that r_descrstructdescr after this assignment r_descrstructdescr ?= r_descr
is cl_abap_structdescr ?
If so why this assignment ? r_descrstructdescr was priviously cl_abap_structdescr too.
Regards
sas
‎2008 Nov 03 9:07 AM
>
> hi,
>
> can you confirm 100 % percent that r_descrstructdescr after this assignment r_descrstructdescr ?= r_descr
>
> is cl_abap_structdescr ?
>
> If so why this assignment ? r_descrstructdescr was priviously cl_abap_structdescr too.
>
> Regards
> sas
The object being referred to by the reference variable doesn't change - it is always a cl_abap_structdescr object.. The type of the reference variable defines what components of the object can be accessed. When you cast, you are changing from a more general type of reference variable, to a more specific type.
matt
‎2008 Nov 03 9:16 AM
hi,
if I understand it correctly then r_descr is being casted to cl_abap_structdescr which was privously cl_abap_typedescr through this
assignment.
r_descrstructdescr ?= r_descr
If so then I have understood it.
Regard
sas
‎2008 Nov 03 9:42 AM
You have understood it. Always try to keep the object and the reference variables as seperate concepts.
matt