Friday, April 5, 2013

Single Event can have Multiple Event Handler Methods

One event can have more than one event handler method in the same class or in different classes. At the run time only one handler method will be triggered at a time. After triggering the one that has to be deactivated and then another handler method will have to be registered to be triggered. In this way system can register multiple event handler method to the same event.

The following program contains a class cls1 where in the public section we declare data v_cls1, event evnt, triggering method trig and event handler method evnthand1 for event evnt of class cls1. Now at the time of implementation we display a text in event handler method evnthand1 then we display another text in triggering method trig.

Next we have defined another class cls2 where in the public section we declare data and another event handler method evnthand2 for event evnt of class cls1. In the implementation we display a text in this method.

Finally in the start of selection we have created object obj1 for cls1 and obj2 for cls2. Now we set handler method evnthand1 for object obj1 and then call triggering method trig. After that we deactivate the registration of evnthand1 and then register again evnthand2 for obj1 by obj2. Then again we call the method trig. Hence initially we have registered the first handler method to the event and call the trigger. Then we have deactivated the registration to register the second handler method and called the same trigger again.

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

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
  PUBLIC SECTION.
    DATA v_cls1 TYPE char50.
    EVENTS evnt.
    METHODS: trig,
             evnthand1 FOR EVENT evnt OF cls1.
ENDCLASS.                    "cls1 DEFINITION

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

*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
  METHOD evnthand1.
    v_cls1 = 'Event Handler in Class One'.
    WRITE / v_cls1.
    SKIP.
  ENDMETHOD.                                                "evnthand1

  METHOD trig.
    v_cls1 = 'Event Triggers in Class One'.
    WRITE / v_cls1.
    RAISE EVENT evnt.
  ENDMETHOD.                    "trig
ENDCLASS.                    "cls1 IMPLEMENTATION

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

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

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

  SET HANDLER: obj1->evnthand1 FOR obj1 ACTIVATION space,
               obj2->evnthand2 FOR obj1.
  CALL METHOD obj1->trig.


Below is the output:

6 comments:

yektek training said...

very thanks for sharing this information reeally nice

Erik Pagaragan said...

Nice!
However, is there any way that the trigger will only be called once? Just a thought. Thanks for sharing!

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training in Chennai

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

http://thecreatingexperts.com/sap-mm-training-in-chennai/

Priya Kannan said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

gilledashivani said...

Great information. the information provided is of great use as i got to learn new things. If you are looking for real time training in SAP ABAP, kindly contact sieve software
Sieve software offers real time training by industry experts with live projects and placement support.
SAP ABAP TRAINING IN HYDERABAD