Application Development 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: 

How to check whether the ref-variable refers to an object or not?

Former Member
0 Kudos

Hi,

I think the topic says all!

Thanks for help!

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate

Do you mean if it is bound?

if o_ref is bound.

Endif.

Regards,

Rich Heilman

3 REPLIES 3

RichHeilman
Developer Advocate
Developer Advocate

Do you mean if it is bound?

if o_ref is bound.

Endif.

Regards,

Rich Heilman

Former Member
0 Kudos

Hi,

Try to assign the data reference to the object reference of type 'object'.. if you are successful then it's object reference!

DATA: lo_obj TYPE REF TO object.

<fs_value> has data reference..

TRY .

lo_obj ?= <fs_value>.

CATCH cx_root.

ENDTRY.

Regards,

Abhijit

<a href="/people/mark.finnern/blog/2004/08/10/spread-the-love the love!</a>

adam_krawczyk1
Contributor
0 Kudos

Hi,

Just to clarify:

  • is bound works only in case if object variable is instantiated (is not initial).
  • casting to object works always.

So if we want to know also if initial variable is object reference type or not we should use casting to object.

Example:

DATA lo_my_class TYPE REF TO zcl_my_class.
DATA lo_test_object TYPE REF TO object.

TRY.

     lo_test_object ?= lo_my_class.
     WRITE 'IS OBJECT casting success'.

CATCH cx_root.
     WRITE 'IS OBJECT casting fail'.

ENDTRY.

DATA l_is_bound TYPE abap_bool.
IF ( lo_my_class IS BOUND ).
     WRITE 'IS OBJECT is bound success'.
ELSE.
     WRITE 'IS OBJECT is bound fail'.
ENDIF.

This will result in output:

IS OBJECT casting success

IS OBJECT is bound fail

Regards,

Adam