Exercise 3: Control Methods in Subprograms
Use
You can also call the methods of a control in a subroutine or in a method. This way, you can isolate a specific function and improve the program structure.
In this exercise, you combine method calls for checking the control status in a subroutine.
Procedure
-
In the PAI module, call subroutine get_lines in branch CASE in which you determine function code PROTECT. Pass parameters from_idx, to_idx, and index by reference:
WHEN 'PROTECT'. DATA: FROM_IDX TYPE I, TO_IDX TYPE I, INDEX TYPE I. PERFORM GET_LINES CHANGING FROM_IDX TO_IDX INDEX. -
Create the subroutine, and move the GET_SELECTION_POS and GET_FIRST_VISIBLE_LINE method calls into this subroutine.
Check Your Work
The program behavior does not change. But your program code has become simpler.
Discussion
Methods GET_SELECTION_POS and GET_FIRST_VISIBLE_LINE import values into actual parameters from_idx, to_idx, and index. The Control Framework stores references to these parameters when it buffers the methods in the automation queue.
Reference Parameters of the Subroutine
The values are assigned to the actual parameters at flush time. In our example, this does not take place within the subroutine. This is rather astonishing since the actual parameters of the methods must be valid at flush time. However, the reference of a parameter in the subroutine turns invalid after the program has ended. Nevertheless, no runtime error occurs.
The reason is that the reference parameters of subroutines are passed on to the Automation Queue (if USING and CHANGING are used). This means that the references are still known when the automation queue is flushed.
Value Parameters of Subroutines
Actual parameters from_idx, to_idx, and index are reused in the PAI module after the subroutine has been processed. In our example, we could therefore not pass the subroutine parameters by call by value - in this case, the return values of methods GET_SELECTION_POS and GET_FIRST_VISIBLE_LINE would be lost.
As a test, we will nevertheless pass one of these parameters by value. To do this, change the form definition as follows:
FORM GET_LINES CHANGING VALUE(FROM_IDX) TO_IDX INDEX.
Start your program and try to protect a line. The program terminates with a runtime error. The reason is that method GET_SELECTION_POS uses local variable FROM_IDX in the form which is not valid at flush time - that is, outside the form.
Consequence: If you use use local variables in a subroutine that are held as references in the Automation Queue, a flush call is required in the subroutine.