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

Getting/comparing the dynamic type of an object

Hello!

is there any way in ABAP to get the dynamic type of an object? In Java we've got the "instanceof" operator. e.g.:

if obj instanceof java.lang.String then
/* It's a String!! */

How can I do this in ABAP?

I've searched google and the ABAP Documentation and several books, but found no hint on that!

Thanks,

Reinhard

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Posted on Mar 31, 2009 at 12:12 PM

    Hi

    You can use the keyword DESCRIBE FIELD.

    DATA: gv_value TYPE p,
          gv_type  TYPE string.
    
    DESCRIBE FIELD: gv_value TYPE gv_type.
    
    WRITE: 'Data Type of gv_value is:', gv_type.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 31, 2009 at 12:14 PM

    Hi,

    You can use the class CL_ABAP_OBJECTDESCR.

    The method DESCRIBE_BY_OBJECT_REF will give you a reference of the class CL_ABAP_CLASSDESCR on which you can call method GET_RELATIVE_NAME or GET_CLASS_NAME.

    Regards,

    Sesh

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi,

      If you only want to check if the instance is of a perticular class or interface then you can also use

      DATA: obj TYPE REF TO <class/interface of your choice>.

      Lets assume lr_obj is the object that you have then you can use...

      TRY.

      obj ?= lv_obj.

      CATCH cx_sy_move_cast_error.

      ENDTRY.

      IF obj IS BOUND.

      " its the object of the type

      ELSE.

      " Its not the object of the type

      ENDIF.

      Regards,

      Sesh

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.