Skip to Content
author's profile photo Former Member
Former Member

casting operation

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_intfdescr

DATA: 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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Nov 03, 2008 at 09: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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 02, 2008 at 07: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

    Add a comment
    10|10000 characters needed characters exceeded

    • 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

  • author's profile photo Former Member
    Former Member
    Posted on Nov 02, 2008 at 02: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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 02, 2008 at 04:32 PM

    Moved to ABAP Objects forum...

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 02, 2008 at 09: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

    Add a comment
    10|10000 characters needed characters exceeded

    • 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.

  • author's profile photo Former Member
    Former Member
    Posted on Nov 02, 2008 at 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

    Add a comment
    10|10000 characters needed characters exceeded

    • >

      > 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

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.