Thursday, April 4, 2013

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:

9 comments:

yektek training said...

nice post thanks for sharing from Swathi

siva sankar Sankar said...

excellent post thanks for sharing from Sankar

Cindy Dy said...

This post has been extremely insightful & useful to increase my knowledge. Well, I'm so happy that I have found this post because I have been seeking some information about it. Here’s a good resource that is also worth a look.

Sol
www.gofastek.com

Leslie Lim said...

I read your blog.I thought it was great.. Hope you have a great day. God bless.

Camille
www.imarksweb.org

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

SieveSoftware said...

Great Post. The information provided is of great use as I got to learn new things. Keep Blogging.
SAP ABAP TRAINING IN HYDERABAD