Copyright 2024 - BV TallVision IT

Imagine a world, where no FORM routine is needed, ever again. How do you capture the bit of logic you would have captured in a form routine ? Use a method. This simple example shows what you need to define a class LCL_CONTROLLER which controls whatever your report should be doing.  

Just for comparison sake, a "hello world" message is shown in classic old style reporting. Then the same is achieved in a controller class. The old style:

REPORT Z_OO_EXAMPLE_1.

START-OF-SELECTION. 
  PERFORM say_hello. 
  
FORM say_hello. 
  MESSAGE 'Hello world' TYPE 'S'. 
ENDFORM. 

The message is thrown without any context, as if it fell out of the sky. Now we will do the same, but capture the "Hello world" message in a controlled class called LCL_CONTROLLER. Note that the LCL_ is quite a common naming convention for local class definitions.

REPORT Z_OO_EXAMPLE_1.

CLASS lcl_controller DEFINITION. 
  PUBLIC SECTION. 

  CLASS-METHODS: say_hello. 
ENDCLASS. 

CLASS lcl_controller IMPLEMENTATION. 
  METHOD say_hello. 
    MESSAGE 'Hello world' TYPE 'S'. 
  ENDMETHOD.
ENDCLASS.  

START-OF-SELECTION.
  lcl_controller=>say_hello( ). 

If it is just "Say hello" your report is supposed to do, then you should consider a version without OO and without even a form-routine. If it's an attempt to see the bigger picture, try these bold statements on the coding above:

  • A class has a DEFINITION and an IMPLEMENTATION block, each with it's own function.
  • The IMPLEMENTATION block is only for actual method coding, so no methods in your DEFINITION block = no IMPLEMENTATION block.
  • Note the PUBLIC SECTION bit, it makes all definitions in the section publically available - more on that later (visibility)
  • Method SAY_HELLO doesn't have any parameters.Parameter settings on methods are a DEFINITION matter.
  • With the above setup, you have implemented a controller class, which should of course be started. In the START-OF-SELECTION the call to the method is done.
  • Note the definition CLASS-METHOD which makes the SAY_HELLO class a class method. Class methods can be called without having to instantiate the class. This also means the method should be called with =>
  • This setup will look like more coding than before at first glance. In practice coding in OO renders much less coding, because the coding itself is put into context.