ABAP Keyword Documentation →  ABAP − Reference →  Creating Objects and Values →  CREATE DATA →  CREATE DATA - AREA HANDLE → 

Creating a Data Object as a Shared Object

The example demonstrates how a data object is created in an area instance version.

Source Code

REPORT demo_create_shared_data_object.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.

    DATA: handle TYPE REF TO cl_demo_area,
          root   TYPE REF TO cl_demo_root,
          exc    TYPE REF TO cx_shm_attach_error.

    FIELD-SYMBOLS <fs> TYPE any.

    DATA(out) = cl_demo_output=>new( ).

    TRY.
        handle = cl_demo_area=>attach_for_write( ).
        CREATE OBJECT root AREA HANDLE handle.
        handle->set_root( root ).
        CREATE DATA root->dref AREA HANDLE handle TYPE string.
        ASSIGN root->dref->* TO <fs>.
        <fs> = `String in shared memory`.
        handle->detach_commit( ).
      CATCH cx_shm_attach_error INTO exc.
        out->display( exc->get_text( ) ).
        LEAVE PROGRAM.
      CATCH cx_shm_external_type.
        out->display( 'Type cannot be used' ).
        LEAVE PROGRAM.
    ENDTRY.

    TRY.
        handle = cl_demo_area=>attach_for_read( ).
        ASSIGN handle->root->dref->* TO <fs>.
        out->display( <fs> ).
        handle->detach( ).
      CATCH cx_shm_attach_error INTO exc.
        out->display( exc->get_text( ) ).
        LEAVE PROGRAM.
    ENDTRY.

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

The AREA HANDLE addition is used to create an anonymous data object of type string in an area instance version of area CL_DEMO_AREA as a shared object. The generically typed attribute dref of area root class CL_DEMO_ROOT is used as a reference variable. A field symbol is used to dereference the data reference and assign a value to the anonymous data object.

Once write access is completed using the DETACH_COMMIT method, read access takes place, which demonstrates how the object is accessed in the shared memory. This type of access can also take place in a different program, provided that the area instance version exists in the shared memory.