Mentor SAP

Event-Controlled Method Calls

An event can have exporting parameters, which means that, in contrast to the explicit method call, the calling program determines the protocol.

 

In this application example, after an instance in the Vehicle class is created, the instance triggers the event Vehicle Created.

 

This event is received in several instances and is processed differently by each instance. For example, the rental car company handles the purchasing of a vehicle, while the vehicle registration office registers the purchased vehicle.   

 

 

Besides attributes and methods, classes, and their instances can also contain another type of component called events.

 

Instance events can be triggered by the instances of the class, but static events can be triggered by the class itself.

 

Events can also be defined as interface components.

 

Given the right circumstances, handler methods react to the triggering of this event. This means that the runtime system may call these handler methods after the event has been triggered. In other words, the client usually does not call the handler method directly.

 

This results in a completely different modeling concept. While you are developing the class that triggers the event, you do not need to know anything about the class that is handling it. The triggering class sends a specific message to all classes and, if required, their instances. At the time of development, type of handlers and the number of handlers, which may be used are not known.

 

Because of the definition of the handler method, the range of possible results can be narrowed down. However, the results, which may occur can be determined only after the event has been triggered.

 

Event Triggering and Handling

Depending on the status of your application, you may not need to program every step every time. The separation of cause and effect in your programming must be reflected in the way you construct complex applications. Often, the event is already triggered, and all you have to do is create another event handler.

 

Within a class, instance events are defined using the EVENTS statement, while static events are defined using the CLASS-EVENTS statement. Events can only have exporting parameters, which must be passed by value.

 

A class or instance can trigger an event at runtime using the RAISE EVENT statement. Both instance events and static events can be triggered in instance methods. In static methods, you can only trigger static events.

 

When an event is triggered, the handler methods that are registered to this event are called in sequence. These handler methods can trigger more events of their own.

 

 

Handling Events – Syntax

Instance events or static methods can be defined within a class to handle events. To do so, you must specify the event using the FOR EVENT statement, and the class or interface in which the event was defined using the OF statement.

If the event contains exporting parameters and you want to be able to address these syntactically, you must have specified the exporting parameters immediately after IMPORTING in the definition of the method. The handler method’s signature exporting parameters are all that are allowed to be included explicitly than the exporting parameters of the associated event. The parameters are typed by the handler method during the definition of the event. The object that triggers the event determines the protocol.

 

In addition to the explicitly defined exporting parameters, the predefined importing parameter SENDER can always be listed. By using that parameter, you can place a reference to the event-trigger object into the handler method.

 

Therefore, handler methods are usually called by triggered events RAISE EVENT. However, they can also be called explicitly (CALL METHOD).

 

 

 

To Trigger and Handle Events

Steps

  1. Define an event in a class. The syntax for this is EVENTS eventname, CLASS-EVENTS eventname.
  2. Trigger the event in a method of this class. The syntax for this is to RAISE EVENT eventname.
  3. Define and implement the handler method in the same or another. The syntax for this is [CLASS -] METHODS … FOR EVENT …. OF.