Skip to Content
Technical Articles
Author's profile photo Mykola Tokariev

ABAP Eventhandling (simple explained)

Events it’s a possibility of the classes to tell anyone who is interested in this, that some changes in the class was made.

Class _A don’t know anything about class _B or class _C. He just send the message for all listener. Like a radio station.

To make listener classes able to get events from the sender class, we must register this event. Like a radio station that you choose in the car to listen all interesting news.

Below you can see some example:

REPORT.
CLASS _a DEFINITION.
  PUBLIC SECTION.
    METHODS change_price
      IMPORTING i_price TYPE price.

    EVENTS price_was_changed.
  PRIVATE SECTION.
    DATA price TYPE price.
ENDCLASS.

CLASS _a IMPLEMENTATION.
  METHOD change_price.
    price = i_price.
    RAISE EVENT price_was_changed.
  ENDMETHOD.
ENDCLASS.


CLASS _b DEFINITION.
  PUBLIC SECTION.
    METHODS handle_price_was_changed FOR EVENT price_was_changed OF _a.
ENDCLASS.

CLASS _b IMPLEMENTATION.
  METHOD handle_price_was_changed.
    WRITE: 'Gotcha!'.
  ENDMETHOD.
ENDCLASS.


CLASS _c DEFINITION.
  PUBLIC SECTION.
    METHODS handle_double_click FOR EVENT double_click OF cl_gui_alv_grid.
ENDCLASS.

CLASS _c IMPLEMENTATION.
  METHOD handle_double_click.
    WRITE `Dont't care about class _a events`.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA(shop) = NEW _a( ).

  DATA(listener_b) = NEW _b( ).
  DATA(listener_c) = NEW _c( ).

  SET HANDLER listener_b->handle_price_was_changed FOR ALL INSTANCES.
  SET HANDLER listener_c->handle_double_click FOR ALL INSTANCES.

  shop->change_price( '10.99' ).

Result. After price was change price_was_changed event was raised. But because we set only one listener (listener_b) to this event, only one handler was triggered.


.

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Michael Keller
      Michael Keller

      Nice explanation! 🙂 In addition to the radio comparison, directional radio is also possible. In this case, SET HANDLER registers handling for an event of a specific instance (see ABAP help).