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: 

Dynamic interface declaration

Former Member
0 Kudos
377

Hello,

Is it possible to have a dynamic interface declaration like you can have a dynamic object declaration?

For an object:

data: lo_object type ref to object.

create object lo_object type (lv_object_name).

Is this in some way possible to do with an interface? I need to do this for an interface at runtime.

Gr,

Jaron

4 REPLIES 4

matt
Active Contributor
0 Kudos
117

As long as lv_object_name contains the name of an interface that exists, then the syntax is valid.

matt

Former Member
0 Kudos
117

I get an error message saying I can't do it because it's not a class.

Edited by: J. Frenk on Nov 16, 2009 3:26 PM

matt
Active Contributor
0 Kudos
117

oops. my bad.

Yes, of course you'll get that error. Because you can't create an object reference to an interface. It has to be reference to a concrete class.

Are you wanting something like:

DATA: r_ref TYPE REF TO (my_interface).

where my_interface contains the name of the interface?

matt

Former Member
0 Kudos
117

You can create data of a dynamically given reference type (which may be an interface) and move the object reference to it.

Something like so:

DATA:
      my_obj TYPE REF TO object,
      lr_data TYPE REF TO data.

  FIELD-SYMBOLS: <fs> TYPE ANY.

  CREATE OBJECT my_obj TYPE my_object_type.

TRY.
  CREATE DATA lr_data TYPE REF TO (some_interface_type_name).
  ASSIGN lr_data->* TO <fs>.
   <fs> ?= my_obj.

  CATCH cx_sy_move_cast_error.
  CATCH cx_sy_create_data_error.
ENDTRY.

Basically, the assignment tests if my_obj is an instance of the type named some_interface_type_name.

Hope this helps,

Sebastian