Show TOC

Events: Introductory ExampleLocate this document in the navigation structure

The following simple example shows the principle of events within ABAP Objects. It is based on the Simple Introduction to Classes . An event critical_value is declared and triggered in class counter.

Tip

REPORT demo_class_counter_event.

CLASS counter DEFINITION.PUBLIC SECTION.    METHODS increment_counter.    EVENTS  critical_value EXPORTING value(excess) TYPE i.  PRIVATE SECTION.    DATA: count     TYPE i,          threshold TYPE i VALUE 10.ENDCLASS.

CLASS counter IMPLEMENTATION.    METHODS increment_counter.    DATA diff TYPE i.    ADD 1 TO count.    IF count > threshold.      diff = count - threshold.      RAISE EVENT critical_value EXPORTING excess = diff.    ENDIF.ENDMETHOD.ENDCLASS.

CLASS handler DEFINITION.PUBLIC SECTION.    METHODS handle_excess             FOR EVENT critical_value OF counter            IMPORTING excess.ENDCLASS.

CLASS handler IMPLEMENTATION.  METHOD handle_excess.    WRITE: / 'Excess is', excess.ENDMETHOD.ENDCLASS.

DATA: r1 TYPE REF TO counter,      h1 TYPE REF TO handler.

START-OF-SELECTION.

 CREATE OBJECT: r1, h1.

 SET HANDLER h1->handle_excess FOR ALL INSTANCES.

 DO 20 TIMES.    CALL METHOD r1->increment_counter.ENDDO.

The class counter implements a counter. It triggers the event critical_value when a threshold value is exceeded, and displays the difference. handlercan handle the exception in counter. During runtime, the handler is registered for all reference variables that point to the object.