Event with Handler Method in Different Class

Event with Handler Method in Different Class


Event can be raised by event handler method which is declared in another class. Here we can metion an event in a class and want to handle that event in another class. In that case we need to declare the triggering method in the first class.

In the following program we have defined a class cls1 where in the public section we have declared data v_cls1, event evnt and method trig. In the implementation part we have implemented the method trig with raising event evnt. We have to declare the triggering method trig in class cls1 because the event evnt is in cls1.

Next we have declared another class cls2 and in the public section we are declaring a data and the event handler method evnthand for event evnt of class cls1. Now in the implementation part we have implemented the evnthand properly. 

Finally in the start of selection we have instantiated the class cls1 and cls2. Now we are calling set handler method evnthand for object of class cls1. Then we call the method trig by object of class cls1.


*&---------------------------------------------------------------------*
*& Report  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
  PUBLIC SECTION.
    DATA v_cls1 TYPE char50.
    EVENTS evnt.
    METHODS trig.
ENDCLASS.                    "cls1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION.
  PUBLIC SECTION.
    DATA v_cls2 TYPE char50.
    METHODS evnthand FOR EVENT evnt OF cls1.
ENDCLASS.                    "cls2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
  METHOD trig.
    v_cls1 = 'Event Triggering Method in Class One'.
    WRITE / v_cls1.
    RAISE EVENT evnt.
  ENDMETHOD.                    "trig
ENDCLASS.                    "cls1 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
  METHOD evnthand.
    v_cls2 = 'Event Handler Method in Class Two'.
    WRITE / v_cls2.
  ENDMETHOD.                    "evnthand
ENDCLASS.                    "cls2 IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj1 TYPE REF TO cls1,
        obj2 TYPE REF TO cls2.

  CREATE OBJECT: obj1, obj2.
  SET HANDLER obj2->evnthand FOR obj1.
  CALL METHOD obj1->trig.


The output is below:

Share this

Related Posts