Events with Handler Method in Same Class
Event handler method can be used in the same class by declaring the event and the handler method with the triggering method. Here the triggering method will raise or trigger the event in the implementation part.
The program below contains a class cls which contains data and constant in the public section. It contains events evnt1 and evnt2 here. And also it has event handler methods evnthand1 and evnthand2. It also contains triggering methods trig1 and trig2.
Now at the time of implementation we implement the event handler methods evnthand1 and evnthand2 with general statement. After that we implement the triggering of event method trig1 and trig2 with RAISE EVENT statement.
Finally in the start of selection we set handler method evnthand1 and evnthand2 by the object. Then we call triggering method trig1 and trig2.
In the debugging mode when the program controller is at CALL METHOD: obj->trig1 then it calls the method trig1 and as the controller goes to RAISE EVENT evnt1 then it goes to event handler method evnthand1. There it follows the general statement. After finishing of that the controller comes back to the triggering method trig1 and end this method. Then it will go for the next triggering method trig2 and next steps will be similar. Here raise event will be evnt2 and event handler method is evnthand2.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
*----------------------------------------------------------------------*
* CLASS cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
PUBLIC SECTION.
DATA v_txt TYPE char50.
CONSTANTS c_das TYPE char50 VALUE '--------------------------------'.
EVENTS: evnt1, evnt2.
METHODS: evnthand1 FOR EVENT evnt1 OF cls,
evnthand2 FOR EVENT evnt2 OF cls,
trig1,
trig2.
ENDCLASS. "cls DEFINITION
*----------------------------------------------------------------------*
* CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
METHOD evnthand1.
v_txt = 'First Event Handler Method'.
WRITE / v_txt.
SKIP 2.
ENDMETHOD. "evnthand1
METHOD evnthand2.
v_txt = 'Second Event Handler Method'.
WRITE / v_txt.
SKIP 2.
ENDMETHOD. "evnthand2
METHOD trig1.
v_txt = 'First Event is being Triggered:'.
WRITE: / v_txt,
/ c_das.
RAISE EVENT evnt1.
ENDMETHOD. "trig1
METHOD trig2.
v_txt = 'Second Event is being Triggered:'.
WRITE: / v_txt,
/ c_das.
RAISE EVENT evnt2.
ENDMETHOD. "trig2
ENDCLASS. "cls IMPLEMENTATION
START-OF-SELECTION.
DATA obj TYPE REF TO cls.
CREATE OBJECT obj.
SET HANDLER: obj->evnthand1 FOR obj,
obj->evnthand2 FOR obj.
CALL METHOD: obj->trig1,
obj->trig2.
The output is following:
The program below contains a class cls which contains data and constant in the public section. It contains events evnt1 and evnt2 here. And also it has event handler methods evnthand1 and evnthand2. It also contains triggering methods trig1 and trig2.
Now at the time of implementation we implement the event handler methods evnthand1 and evnthand2 with general statement. After that we implement the triggering of event method trig1 and trig2 with RAISE EVENT statement.
Finally in the start of selection we set handler method evnthand1 and evnthand2 by the object. Then we call triggering method trig1 and trig2.
In the debugging mode when the program controller is at CALL METHOD: obj->trig1 then it calls the method trig1 and as the controller goes to RAISE EVENT evnt1 then it goes to event handler method evnthand1. There it follows the general statement. After finishing of that the controller comes back to the triggering method trig1 and end this method. Then it will go for the next triggering method trig2 and next steps will be similar. Here raise event will be evnt2 and event handler method is evnthand2.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
*----------------------------------------------------------------------*
* CLASS cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
PUBLIC SECTION.
DATA v_txt TYPE char50.
CONSTANTS c_das TYPE char50 VALUE '--------------------------------'.
EVENTS: evnt1, evnt2.
METHODS: evnthand1 FOR EVENT evnt1 OF cls,
evnthand2 FOR EVENT evnt2 OF cls,
trig1,
trig2.
ENDCLASS. "cls DEFINITION
*----------------------------------------------------------------------*
* CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
METHOD evnthand1.
v_txt = 'First Event Handler Method'.
WRITE / v_txt.
SKIP 2.
ENDMETHOD. "evnthand1
METHOD evnthand2.
v_txt = 'Second Event Handler Method'.
WRITE / v_txt.
SKIP 2.
ENDMETHOD. "evnthand2
METHOD trig1.
v_txt = 'First Event is being Triggered:'.
WRITE: / v_txt,
/ c_das.
RAISE EVENT evnt1.
ENDMETHOD. "trig1
METHOD trig2.
v_txt = 'Second Event is being Triggered:'.
WRITE: / v_txt,
/ c_das.
RAISE EVENT evnt2.
ENDMETHOD. "trig2
ENDCLASS. "cls IMPLEMENTATION
START-OF-SELECTION.
DATA obj TYPE REF TO cls.
CREATE OBJECT obj.
SET HANDLER: obj->evnthand1 FOR obj,
obj->evnthand2 FOR obj.
CALL METHOD: obj->trig1,
obj->trig2.
The output is following:
Post a Comment