Lilienthal
Lilienthal

Reputation: 4378

Variable Encapsulation in Case Statement

While modifying an existing program's CASE statement, I had to add a second block where some logic is repeated to set NetWeaver portal settings. This is done by setting values in a local variable, then assigning that variable to a Changing parameter. I copied over the code and did a Pretty Print, expecting to compiler to complain about the unknown variable. To my surprise however, this code actually compiles just fine:

CASE i_actionid.
    WHEN 'DOMIGO'.
        DATA:   ls_portal_actions  TYPE powl_follow_up_sty.
        CLEAR ls_portal_actions.
        ls_portal_actions-bo_system = 'SAP_ECC_Common'.
        " [...]
        c_portal_actions = ls_portal_actions.
    WHEN 'EBELN'.
        ls_portal_actions-bo_system = 'SAP_ECC_Common'.
        " [...]
        C_PORTAL_ACTIONS = ls_portal_actions.
ENDCASE.

As I have seen in every other programming language, the DATA: declaration in the first WHEN statement should be encapsulated and available only inside that switch block. Does SAP ignore this encapsulation to make that value available in the entire CASE statement? Is this documented anywhere?

Note that this code compiles just fine and double-clicking the local variable in the second switch takes me to the data declaration in the first. I have however not been able to test that this code executes properly as our testing environment is down.

Upvotes: 5

Views: 1368

Answers (3)

Jagger
Jagger

Reputation: 10524

The inline variable declarations are now possible with the newest version of SAP Netweaver. Here is the link to the documentation DATA - inline declaration. Here are also some guidelines of a good and bad usage of this new feature

Here is a quote from this site:

A declaration expression with the declaration operator DATA declares a variable var used as an operand in the current writer position. The declared variable is visible statically in the program from DATA(var) and is valid in the current context. The declaration is made when the program is compiled, regardless of whether the statement is actually executed.

Personally have not had time to check it out yet, because of lack of access to such system.

Upvotes: 0

vwegert
vwegert

Reputation: 18483

Your assumption that a CASE statement defines its own scope of variables in ABAP is simply wrong (and would be wrong for a number of other programming languages as well). It's a bad idea to litter your code with variable declarations because that makes it awfully hard to read and to maintain, but it is possible. The DATA statements - as well as many other declarative statements - are only evaluated at compile time and are completely ignored at runtime. You can find more information about the scopes in the online documentation.

Upvotes: 3

DW8Reaper
DW8Reaper

Reputation: 521

In short you cannot do this. You will have the following scopes in an abap program within which to declare variables (from local to global):

  • Form routine: all variables between FORM and ENDFORM
  • Method: all variables between METHOD and ENDMETHOD
  • Class - all variables between CLASS and ENDCLASS but only in the CLASS DEFINITION section
  • Function module: all variables between FUNCTION and ENDFUNCTION
  • Program/global - anything not in one of the above is global in the current program including variables in PBO and PAI modules

Having the ability to define variables locally in a for loop or if is really useful but unfortunately not possible in ABAP. The closest you will come to publicly available documentation on this is on help.sap.com: Local Data in the Subroutine

As for the compile process do not assume that ABAP will optimize out any variables you do not use it won't, use the code inspector to find and remove them yourself. Since ABAP works the way it does I personally define all my variables at the start of a modularization unit and not inline with other code and have gone so far as to modify the pretty printer to move any inline definitions to the top of the current scope.

Upvotes: 9

Related Questions