ABAP - Keyword Documentation →  ABAP - Reference →  Calling and leaving program units →  Calling Processing Blocks →  Call Event Handler →  SET HANDLER → 

SET HANDLER - FOR

Quick Reference

Syntax

SET HANDLER handler1 handler2 ... FOR { oref |{ALL INSTANCES} }
                                  [ACTIVATION act].

Extras:

1. ... FOR oref

2. ... FOR ALL INSTANCES

3. ... ACTIVATION act

Effect

This statement registers the event handlers handler1 handler2 ... for the associated instance events of the objects specified after FOR. The addition ACTIVATION can be used to deregister event handlers or perform a dynamic registration.

An event handler is raised if the associated instance event is raised using RAISE EVENT in an object for which it is registered. An event handler handler can be specified as follows, where the names have the same meaning as in the explicit method call:

Methods meth can be specified from the same class or from other classes defined as instance event handlers using the addition FOR EVENT evt OF {class|intf} of the statements [CLASS-]METHODS. No event handlers for static events can be specified. At least one name must be specified.

The type class or intf specified after FOR EVENT OF in the definition of an instance event handler defines the objects whose events it can handle. Single objects or all handleable objects can be specified after FOR.

Addition 1

... FOR oref

Effect

This addition registers or deregisters the event handlers of the list handler1 handler2 ... for exactly one object. oref is an object reference that must point to an object whose events can be handled by the specified event handlers. The class of the object must be class or a subclass of class, or must implement the interface intf directly or through a superclass.

oref is a functional operand position.

Example

Registers an event handler for an ALV event.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    METHODS main.
  PRIVATE SECTION.
    DATA itab TYPE TABLE OF scarr.
    METHODS handle_double_click
            FOR EVENT double_click OF cl_salv_events_table.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA alv TYPE REF TO cl_salv_table.
    ...
    TRY.
        cl_salv_table=>factory(
          IMPORTING r_salv_table = alv
          CHANGING  t_table      = itab ).
        SET HANDLER handle_double_click FOR alv->get_event( ).
      CATCH cx_salv_msg.
        ...
    ENDTRY.
  ENDMETHOD.
  METHOD handle_double_click.
     ...
  ENDMETHOD.
ENDCLASS.

Addition 2

... FOR ALL INSTANCES

Effect

This addition registers or deregisters the event handlers of the list handler1 handler2 ... for all objects whose events they can handle. These are all objects whose classes are either class or the subclass of class, or which implement the interface intf directly or through a superclass. A registration of this type is also valid for all raising instances created after the statement SET HANDLER.

Note

Registration with FOR ALL INSTANCES applies also in particular for temporary instances as they can be created when using the instantiaion operator NEW.

Addition 3

... ACTIVATION act

Effect

A single-character text-like field act can be specified after the addition ACTIVATION. If act has the value "X" (the default value), the event handlers handler are registered. If act has the value " ", however, the registration of the event handlers handler is canceled. A single registration cannot, on the other hand, be deregistered using mass deregistration. Conversely, individual raising objects cannot be excluded from registration after a mass registration.

Note

As long as the registration of an instance method as an event handler for an instance event is not canceled using ACTIVATION " " or all raising instances are deleted, the associated object cannot be deleted by the garbage collector. This is because it is still used by the runtime environment.

Example

Registers an event handler with FOR ALL INSTANCES. The events of all temporary instances created with NEW are handled until registration is stopped. The program can be executed as DEMO_SET_HANDLER_FOR_ALL.

CLASS cls DEFINITION.
  PUBLIC SECTION.
    EVENTS evt
      EXPORTING VALUE(p) TYPE string DEFAULT `nop`.
    METHODS meth
      IMPORTING p TYPE string.
ENDCLASS.

CLASS cls IMPLEMENTATION.
  METHOD meth.
    RAISE EVENT evt EXPORTING p = p.
  ENDMETHOD.
ENDCLASS.

CLASS hdl DEFINITION.
  PUBLIC SECTION.
    METHODS meth FOR EVENT evt OF cls
      IMPORTING p.
ENDCLASS.

CLASS hdl IMPLEMENTATION.
  METHOD meth.
    cl_demo_output=>write( p ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA(href) = NEW hdl( ).
  SET HANDLER href->meth FOR ALL INSTANCES.

  NEW cls( )->meth( `Ping 1`).
  NEW cls( )->meth( `Ping 2`).
  NEW cls( )->meth( `Ping 3`).

  SET HANDLER href->meth FOR ALL INSTANCES ACTIVATION ' '.

  NEW cls( )->meth( `Ping 4`).
  NEW cls( )->meth( `Ping 5`).
  NEW cls( )->meth( `Ping 6`).

  cl_demo_output=>display( ).