Event parameters in ABAP Objects

Go down

Event parameters in ABAP Objects

Post  Uma_ABAP on Sun Apr 01, 2012 12:52 pm


Like methods parameters can be passed to events, events can only have input parameters;

any no of parameters can be passed to an event. As we know an event does not have any functionality, (the functionality associated with an event is carried out by the event handler method that has been assigned to it) then the question is what does the event do with the input parameters?

The parameters that are passed to the events is actually utilized by the handler methods that are registered to the events, the handler methods have the liberty to decide on the number of input parameters passed by the events .The number of input parameters for the handler methods has to be defined explicitly. However, an event handler method is not obliged to handle all of the actual parameters that are passed to it.

As well as the explicitly defined parameters, each instance event also contains the implicit EXPORTING parameter SENDER. This has the type of a reference variable referring to the class or interface in which the event is declared. If you include the SENDER parameter in the IMPORTING parameter list of the handler method, the method receives a reference to the triggering object. Remember, however, that teh type of the reference variable is not necessarily the class of the sending object. If the event is declared in a superclass of the class of the sender, or in an interface implemented by it, the type of the reference variable will refer to the superclass or interface instead. Static events do not have a SENDER parameter.

Few points to note

Event parameters
are always passed by VALUE
Must always have a type specification (TYPE, LIKE)
Can be optional (OPTIONAL, DEFAULT)

Code:

Let’s take an example
CLASS evt_container DEFINITION.
PUBLIC SECTION.
METHODS event_trigger.
EVENTS my_event EXPORTING VALUE(PAR1) TYPE I
VALUE(PAR2) TYPE I OPTIONAL
VALUE(PAR3) TYPE I DEFAULT 3.
ENDCLASS.
CLASS class_evt_handler_method DEFINITION.
PUBLIC SECTION.
METHODS handler_for_event FOR EVENT my_event OF evt_container IMPORTING
SENDER PAR3.
ENDCLASS.
CLASS evt_container IMPLEMENTATION.
METHOD evt_trigger.
RAISE EVENT my_event EXPORTING PAR1 = 1.
ENDMETHOD.
ENDCLASS.
CLASS class_event_handler_method IMPLEMENTATION.
METHOD handler_my_event.
Write :/ ‘ this is event handler method’.
ENDMETHOD.
ENDCLASS.
*************** main program *******************************
DATA:ref_obj1 TYPE REF TO evt_container,
Ref_obj2 TYPE REF TO class_evt_handler_method.
Create object : ref_obj1,
Ref_obj2.
SET HANDLER ref_obj2->handler_for_event FOR evt_container.
Call method ref_obj1->evt_trigger. “ this will raise the event.
“event handler method will be called automatically
*************** out put ***********************************
this is event handler method

Uma_ABAP

Posts : 56
Join date : 2012-03-31

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum