Exercise 2: Passing Values Between Methods

Use

Method parameters that are passed by reference (call by reference) can be passed on in the automation queue. Thus methods can use return values of preceding methods in the queue.

For this exercise, we have designed an example in which this option is not desired. You implement a query that disallows users to protect the first visible line in the text window. If they attempt to do this, users should get an error message.

Procedure

In the PAI module, insert the following lines of code before the LOOP AT mytable INTO textstruct line, and create the text element with an appropriate error message:

CALL METHOD EDITOR->GET_FIRST_VISIBLE_LINE
         IMPORTING
             LINE = INDEX
         EXCEPTIONS
             ERROR_CNTL_CALL_METHOD = 1.
IF FROM_IDX = INDEX.
    MESSAGE i208(00) WITH TEXT-003.
    EXIT.
ENDIF.

Check Your Work

Start your program, import the internal table and select any area you like on the text window. However, rather than displaying an error message, the system protects the first visible line.

Discussion

There are two reasons for this wrong behavior: First, the INDEX variable is used multiple times, and second, the methods are executed at different times (due to space restrictions, the graphic does not show parameter ENABLE_EDITING_PROTECTED_TEXT of method PROTECT_LINES):

Reading the Imported Value

The fact that the error message fails to appear has the following reason: In the IF query, INDEX still has its initial value of 0 since the get_first_visible_line method is only executed at flush time. This is why the error message is not displayed. Any time the user retries to protect lines, INDEX always has the value of the most recent CASE branch processing run. This effect has been described in the previous exercise.

Passing Parameters Within the Automation Queue

The fact that the wrong lines are protected has the following reason: In the loop, one or more PROTECT_LINES method calls are added to the automation queue, depending on the values of the FROM_IDX and TO_IDX variables. The actual parameters are stored as references since the get_first_visible_line method has previously imported the same INDEX parameter. At runtime, INDEX is then passed on to the actual parameters of the PROTECT_LINES method.

The automation queue does not adopt the reference parameter values until flush time. This is why the INDEX = SY-TABIX assignment does not have any practical effect. In addition, the actual parameters of method PROTECT_LINES are overwritten after get_first_visible_line returns the value of LINE.

Corrective Action

If you do not want to pass on the return value of a method within the automation queue, you can:

  • Introduce another variable, or

  • Use another flush.

The get_selection_pos and get_first_visible_line method calls are independent of each other. Therefore, add both to the automation queue. To do this, move the flush call that comes after the GET_SELECTION_POS method before the IF block for displaying the message.

Then, the selected lines are protected, and the error message created is displayed as a dialog box for the first visible line.