SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

Object Oriented Programming in ABAP - Events with Handler Method in the same class

Object Oriented Programming in ABAP - Events with Handler Method in the same class

Event is a mechanism by which method of one class can raise method of another class, without the hazard of instantiating that class .
The steps to be followed are as follows:-

  • Create an event in a class
  • Create a triggering method in the same class which will raise the event.
  • Create an event handler method for the event in same/other class.
  • Register the event handler method in the program.

Now, your settings are complete. Create an object from the class containing the event and call the triggering method to raise the event.

  • Class C1 contains an event E1, for which the triggering method is T1.
  • Event handler method for event E1 is M1, placed in the same class C1.
  • Registration is done at runtime for M1.
  • Object is created from class C1 and the triggering method T1 is called, which raises the event and ultimately calls event handler method M1.


REPORT zstp_oo_abap.

CLASS c1 DEFINITION.
  PUBLIC SECTION.
*(1)Creating event : E1
    EVENTS: e1.
*(2) Creating an event handling method. This method can belong to
* same or different class
    METHODS: m1 FOR EVENT e1 OF  c1.
* Method to raise the event
    METHODS : t1.
ENDCLASS.

CLASS c1 IMPLEMENTATION.
* Method : M1 will be called when the event is raised
  METHOD : m1.
    WRITE:/5 ' I am the event handler method'.
  ENDMETHOD.
* Method : T1 will raise the event
  METHOD : t1.
    WRITE:/5 'I am T1, going to raise event E1'.
    RAISE EVENT e1.
  ENDMETHOD.
ENDCLASS.


START-OF-SELECTION.
  DATA: oref TYPE REF TO c1.
  CREATE OBJECT: oref .
* Registering the event handler method
  SET HANDLER oref->m1 FOR oref  .
* Calling the event which will raise the event.
  CALL METHOD oref->t1.

Output -

Object Oriented Programming in ABAP - Events with Handler Method in the same class

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in