Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

RE:ABAP objects

alex_georgek
Explorer
0 Kudos

Hi all,

Can anyone explain with a simple example what is the purpose

for SET HANDLER in OOPs ABAP.

Thanks in advance,

Alex.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

hi,

Creating an instance in the class using CREATE OBJECT obj, where obj has the data type

REF TO <classname>.

Addressing a static attribute using <classname>=><an_attribute>.

Calling a static attribute using CALL METHOD <classname>=><a_classmethod>.

Registering a static event handler method using SET HANDLER <classname>=><handler_method> for

obj.

Registering an event handler method for a static event in class <classname>.

The static constructor cannot be called explicitly.

Events are registered using the command SET HANDLER. Registration is only active at program runtime.

Events cannot be persistent.

You want to register an object to an event belonging to another object. The SET HANDLER... statement

enters the registration in that object’s list. All handlers for one event are entered in this list.

When the event is triggered, the list shows which event handler methods need to be called.

When an event is triggered, only those event handler methods that have registered themselves using SET

HANDLER by this point at runtime are executed.

You can register an event using Activation ‘X‘ (see above example), and deregister it using Activation

‘SPACE‘ (see next slide). You can also register and deregister using a variable <var>, which is filled with

one of these two values. If you do not specify a value for Activation, then the event is registered (default

setting).

Immediate effects of SET HANDLER on event handler

methods:

Newly registered event handlers are also executed.

Deregistered handlers may already have been executed.

The visibility in an event handler method establishes

authorization for SET-HANDLER statements.

Event Event handler method

public -


public, protected, private

protected ---protected, private

private -


private

Events are also subject to the visibility concept and can therefore be either public, protected or private.

Visibility establishes authorization for event handling :

all users

only users within that class or its subclasses

only users in that class.

Event handler methods also have visibility characteristics. Event handler methods, however, can only have

the same visibility or more restricted visibility than the events they refer to.

The visibility of event handler methods establishes authorization for SET-HANDLER statements: SET

HANDLER statements can be made

anywhere

in that class and its subclasses

only in that class

Hope this helps, Do reward.

5 REPLIES 5

Former Member
0 Kudos

SET HANDLER is used in the event handling for ABAP objects.

1. Event is triggered by an object to inform a change of state.

2. The objects that are affected or bound to be affected by an event subscribe to the event and process it, that is react in some particular way to the change of state.

So, this is the procedure.

1. At the moment of implementation, a class (say A) defines its

- instance events (using the statement EVENTS) and

- static events (using the statement CLASS-EVENTS)

Class A or its instance raises an event by using the statement RAISE EVENT.

2. Classes (can be different from the class that has triggered the event, say B and C) or their instances that receive a message when an event is triggered at runtime and want to react to this event define event handler methods. So, in B and C, define the following

Statement : (CLASS-)METHODS <handler_method> FOR EVENT <event> OF <classname>.

3. These classes (B or C) or their instances register themselves at runtime to one or more events. Possibly in B or C's constructor or in a method belonging to B or C.

Statement : SET HANDLER <handler_method> FOR <reference>. (for instance events)

SET HANDLER <handler_method>. (for static events).

Check the following code.

*----


Step 1. -


CLASS lcl_airplane DEFINITION.

PUBLIC SECTION.

METHODS arrive_at_airport.

EVENTS touched_down EXPORTING VALUE(ex_name) TYPE string.

PRIVATE SECTION.

DATA: name TYPE string.

ENDCLASS.

CLASS lcl_airplane IMPLEMENTATION.

METHOD arrive_at_airport.

...

RAISE EVENT touched_down EXPORTING ex_name = name.

ENDMETHOD.

ENDCLASS.

*----


Step 2. -


CLASS lcl_air_traffic_controller DEFINITION.

...

PRIVATE SECTION.

METHODS: on_touched_down FOR EVENT touched_down OF lcl_airplane

IMPORTING ex_name

SENDER.

ENDCLASS.

*----


Step 3. -


CLASS lcl_air_traffic_controller IMPLEMENTATION.

METHOD add_airplane.

SET HANDLER on_touched_down FOR im_plane ACTIVATION `X`. ...

ENDMETHOD.

METHOD ...

ENDCLASS.

-


Some more information on SET HANDLER.

1.Events are registered using the command SET HANDLER. Registration is only active at program

runtime.

2. Events cannot be persistent.

3. You want to register an object to an event belonging to another object. The SET HANDLER...

statement enters the registration in that object’s list. All handlers for one event are entered in this list.

4. When an event is triggered, only those event handler methods that have registered themselves using

SET HANDLER by this point at runtime are executed.

5. You can register an event using Activation ‘X‘ and deregister it using Activation ‘SPACE‘ . You can also register and deregister using a variable <var>, which is filled with one of these two values.

For deregistration.

SET HANDLER on_touched_down FOR SENDER ACTIVATION SPACE.

If you do not specify a value for Activation, then the event is registered (default setting).

6. You can register several methods in one SET-HANDLER statement:

SET HANDLER <ref_handle1>-><handler_method1> ... <ref_handlen>-><handler_methodN> FOR <ref_sender> | FOR ALL INSTANCES.

7. When the event is triggered, the list shows which event handler methods need to be called.

8. Every object that has defined events has an internal table: the handler table. All objects that have

registered for events are entered in this table together with their event handler methods. Objects that have registered themselves for an event that is still “live” also remain “live”. The methods of these objects are called when the event is triggered, even if they can no longer be reached using main memory references.

Former Member
0 Kudos

Former Member
0 Kudos

hi,

Creating an instance in the class using CREATE OBJECT obj, where obj has the data type

REF TO <classname>.

Addressing a static attribute using <classname>=><an_attribute>.

Calling a static attribute using CALL METHOD <classname>=><a_classmethod>.

Registering a static event handler method using SET HANDLER <classname>=><handler_method> for

obj.

Registering an event handler method for a static event in class <classname>.

The static constructor cannot be called explicitly.

Events are registered using the command SET HANDLER. Registration is only active at program runtime.

Events cannot be persistent.

You want to register an object to an event belonging to another object. The SET HANDLER... statement

enters the registration in that object’s list. All handlers for one event are entered in this list.

When the event is triggered, the list shows which event handler methods need to be called.

When an event is triggered, only those event handler methods that have registered themselves using SET

HANDLER by this point at runtime are executed.

You can register an event using Activation ‘X‘ (see above example), and deregister it using Activation

‘SPACE‘ (see next slide). You can also register and deregister using a variable <var>, which is filled with

one of these two values. If you do not specify a value for Activation, then the event is registered (default

setting).

Immediate effects of SET HANDLER on event handler

methods:

Newly registered event handlers are also executed.

Deregistered handlers may already have been executed.

The visibility in an event handler method establishes

authorization for SET-HANDLER statements.

Event Event handler method

public -


public, protected, private

protected ---protected, private

private -


private

Events are also subject to the visibility concept and can therefore be either public, protected or private.

Visibility establishes authorization for event handling :

all users

only users within that class or its subclasses

only users in that class.

Event handler methods also have visibility characteristics. Event handler methods, however, can only have

the same visibility or more restricted visibility than the events they refer to.

The visibility of event handler methods establishes authorization for SET-HANDLER statements: SET

HANDLER statements can be made

anywhere

in that class and its subclasses

only in that class

Hope this helps, Do reward.

Former Member
0 Kudos

hI

Registers and deregisters Event Handler Methods dynamically at Runtime

This statement can be used in three ways

SET HANDLER h1 ….hn FOR ref

This form is used for instance events and registers the Event Handler Methods for only one instance (i.e one object )

ref stands for Class Reference Variable or Interface Reference Variable

SET HANDLER h1…..hn FOR ALL INSTANCES

This form is used for instance events and registers the Event Handler Methods for all Instances

ref stands for Class Reference Variable or Interface Reference Variable

SET HANDLER h1…..hn.

This form is used for static events.

In case of Class Reference : It registers Event Handler Methods for triggering from class where the event has been declared.

In case of Interface Reference : It registers the event handler methods for all triggering classes that implement the interface intf.

It triggers the Event Handler Methods that have been registered for that event with the SET HANDLER statement

Former Member
0 Kudos

Hi

Registering Event Handler Methods

To allow an event handler method to react to an event, you must determine at runtime the trigger to which it is to react. You can do this with the following statement:

SET HANDLER h1 h2 ... ...

It links a list of handler methods with corresponding trigger methods. There are four different types of event.

It can be

· An instance event declared in a class

· An instance event declared in an interface

· A static event declared in a class

· A static event declared in an interface

The syntax and effect of the SET HANDLERdepends on which of the four cases listed above applies.

For an instance event, you must use the FOR addition to specify the instance for which you want to register the handler. You can either specify a single instance as the trigger, using a reference variable ref:

SET HANDLER h1 h2 ... FOR ref.

or you can register the handler for all instances that can trigger the event:

SET HANDLER h1 h2 ... FOR ALL INSTANCES.

The registration then applies even to triggering instances that have not yet been created when you register the handler.

You cannot use the FOR addition for static events:

SET HANDLER h1 h2 ...

The registration applies automatically to the whole class, or to all of the classes that implement the interface containing the static event. In the case of interfaces, the registration also applies to classes that are not loaded until after the handler has been registered.

REPORT ZEVENT_HANDLER3 .

CLASS c1 DEFINITION.PUBLIC

SECTION.

  • Creating event : E1

EVENTS: E1.

  • Creating an event handling method.

METHODS: M1 FOR EVENT E1 OF c1.

  • Method to raise the event

METHODS : T1.

ENDCLASS.

CLASS C2 DEFINITION.

PUBLIC SECTION.

  • Creating an event handling method.

METHODS: M2 FOR EVENT E1 OF c1.

endclass.

CLASS c1 IMPLEMENTATION.

  • Method : T1 will raise the event

METHOD : T1.

write:/5 'I am T1, going to raise event E1'.

raise event E1.

ENDMETHOD.

  • Method : M1 will be called when the event is raised

METHOD : M1.

write:/5 ' I am the event handler method M1 in c1'.

ENDMETHOD.

ENDCLASS.

class c2 implementation.

  • Method : M2 will be called when the event is raised

METHOD : M2.

write:/5 ' I am the event handler method M2 in c2'.

ENDMETHOD.

endclass.

Start-of-selection.

Data: oref1 type ref to c1,

oref2 type ref to c2.

Create object: oref1 , oref2.

  • Registering the event handler method

SET HANDLER oref1->M1 FOR oref1.

  • Calling the event which will raise the event.

call method oref1->T1.

  • De-Registering the earlier event handler method

SET HANDLER oref1->M1 FOR oref1 ACTIVATION space .

  • Registering the new event handler method

SET HANDLER oref2->M2 FOR oref1 .

  • Calling the event which will raise the event.

call method oref1->T1.