Mentor SAP
2017-07-04 Submitted by:- Admin

An event can be raised by event handler method which is declared in another class. Here we can mention 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 ABC wherein the public section we have declared data V_ABC, 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 ABC because the event evnt is on ABC.

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

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

 

REPORT zevent_differentclass.

*----------------------------------------------------------------------*
*       CLASS ABC DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS abc DEFINITION.
  PUBLIC SECTION.
    DATA v_abc TYPE char50.
    EVENTS evnt.
    METHODS trig.
ENDCLASS.                    "ABC DEFINITION

*----------------------------------------------------------------------*
*       CLASS XYZ DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS xyz DEFINITION.
  PUBLIC SECTION.
    DATA v_xyz TYPE char50.
    METHODS evnthand FOR EVENT evnt OF abc.
ENDCLASS.                    "XYZ DEFINITION

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

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

* Start of selection
START-OF-SELECTION.

* Data Declaration
  DATA: obj1 TYPE REF TO abc,
        obj2 TYPE REF TO xyz.

* Create object
  CREATE OBJECT: obj1, obj2.

* Set handler
  SET HANDLER obj2->evnthand FOR obj1.

* Trigger method
  CALL METHOD obj1->trig.

 

OUTPUT