Summary -

Events are functions that are triggered based on the result of a condition. Events are used by Objects or classes to trigger event handler methods in other objects or classes. Any number of event handler methods can be called when an event is triggered. The link between a trigger and its handler method is decided dynamically at run-time.

The class events can trigger in the methods of the same class using the RAISE EVENT statement. Events have a similar parameter interface to methods, but only have output parameters.

Events are the effective way to communicate between objects. Events are the message raised by an object. The event can caught by the receiver and the receiver implements the logic to handle the event.

Events are two types and those are -

Event Types Description
Instance Events Instance events can be declared using the EVENTS statement. An instance event can only gets triggered in an instance method.

EVENTS <event-name> EXPORTING... 
			VALUE (e1 e2 ...) TYPE type [OPTIONAL]..
Static Events Static events can be declared using the CLASS-EVENTS statement. All instance and static methods can trigger static events. Static events are the only event that can be triggered in a static method.

CLASS-EVENTS <event-name>...

A class event can trigger an event handler method of the same class by using the RAISE EVENT statement. When an event is triggered, appropriate event handler methods are supposed to be executed in all the handling classes.

Event Definition -

The event can be defined by using the EVENT clause and the syntax of event class shown below -

CLASS <class-name> DEFINITION.
    EVENTS: <event-name>
ENDCLASS. 

Raise an Event -

Raising an event can be done via RAISE EVENT and the syntax of the raise event shown below -

RAISE EVENT <event-name>. 

Event Handler Definition -

The event handler method for an event can be defined by using the FOR EVENT clause. The event class defintion syntax shown below -

CLASS <class-name> DEFINITION.
  PUBLIC SECTION.
    METHODS: <method-name>
      FOR EVENT <event-name> OF <class-name> IMPORTING e1 e2 ….
ENDCLASS. 

Setting up the handler -

An event is linked to its handler method dynamically in a program by using the SET HANDLER statement. The SET HANDLER syntax shown below -

CREATE OBJECT <object-name>
SET HANDLER <new-object-name> -> <method-name> FOR <object-name>.


Example -

Simple example to explain how the event is defined and called.


Code -

*&---------------------------------------------------------------------*
*& Report  Z_EVENTS
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_EVENTS.

*Class definition
CLASS classnew DEFINITION.
PUBLIC SECTION.
* Creating an event: eventnew.
  EVENTS eventnew.
* Creating an event handling method: methodnew.
  METHODS: methodnew FOR EVENT eventnew OF classnew.
* Creating method to raise an event.
  METHODS: triggernew.
ENDCLASS.

*Class implementation
CLASS classnew IMPLEMENTATION.
* methodnew will be called when an event is raised.
  METHOD methodnew.
    WRITE:/ 'Executing event handler method'.
  ENDMETHOD.
* triggernew will raise the event.
  METHOD triggernew.
    WRITE:/ 'Executing trigger method to raise an event'.
    RAISE EVENT eventnew.
  ENDMETHOD.
ENDCLASS.

* Creatng class object
START-OF-SELECTION.
  DATA: obj TYPE REF TO classnew.
  CREATE OBJECT obj.
* Registering the event handler method.
  SET HANDLER obj->methodnew for obj.
* Calling event method which raises the event.
  CALL METHOD obj->triggernew.

Output -

Class Events Example Output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

eventnew is an event and methodnew is an event handling method. triggernew is a method to raise an event. Calling an event method raises the event after registering the event handler method with the object.