Application Development and Automation 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: 
Read only

Clone an Instance

Former Member
0 Likes
1,317

Is it possible to clone an instance of an object object like

cl_salv_tree?

2 REPLIES 2
Read only

Former Member
0 Likes
493

I could not find any way to clone an instance.

Read only

uwe_schieferstein
Active Contributor
0 Likes
493

Hello Gregory

The following sample report <b>ZUS_SDN_CLONE_INSTANCE</b> shows how to clone instances.

To me it seems a rather crude way (making a SYSTEM-CALL) but apparently the object you wish to clone has to implement interface IF_OS_CLONE. Perhaps there is a more elegant way available which I am not aware of yet.

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_CLONE_INSTANCE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_clone_instance.



*---------------------------------------------------------------------*
*       CLASS lcl_foo DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_foo DEFINITION.

  PUBLIC SECTION.
    INTERFACES:
      if_os_clone.
    DATA:
      md_counter      TYPE i,
      md_description  TYPE as4text.

    METHODS:
      constructor
        IMPORTING
          value(id_counter)  TYPE i
          value(id_text)     TYPE as4text.

ENDCLASS.                    "lcl_foo DEFINITION


*---------------------------------------------------------------------*
*       CLASS lcl_foo IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_foo IMPLEMENTATION.

  METHOD constructor.
    me->md_counter     = id_counter.
    me->md_description = id_text.
  ENDMETHOD.                    "constructor

  METHOD if_os_clone~clone.
************************************************************************
*
* Purpose       : Creates a clone of the object.
*
************************************************************************

    SYSTEM-CALL OBJMGR CLONE me TO result.
  ENDMETHOD.                    "if_os_clone~clone
ENDCLASS.                    "lcl_foo IMPLEMENTATION



DATA:
  go_foo       TYPE REF TO lcl_foo,
  go_foo_1     TYPE REF TO lcl_foo,
  go_obj       TYPE REF TO object,
  go_state     TYPE REF TO cl_os_state.



START-OF-SELECTION.


  CREATE OBJECT go_foo
    EXPORTING
      id_counter = 1
      id_text    = 'Description of instance'.

  WRITE: / 'GO_FOO:', go_foo->md_counter, go_foo->md_description.


  go_obj = go_foo->if_os_clone~clone( ).
  go_foo_1 ?= go_obj.

  WRITE: / 'Clone :', go_foo_1->md_counter, go_foo_1->md_description.

END-OF-SELECTION.

Regards

Uwe