"
Supported by

Upside down CASE

What is your favourite colour?


SELECTION-SCREEN BEGIN OF BLOCK b1.
PARAMETERS: p_azul BUTTONGROUP GROUP COR DEFAULT 'X',
            p_verde BUTTONGROUP GROUP COR,
            p_roxo BUTTONGROUP GROUP COR.
SELECTION-SCREEN END OF BLOCK b1.

If you said ‘blue’ you live and can cross the bridge.

In any case, the following normally happens in ABAP to discover the colour the user chose:


IF p_azul = 'X'.
  lv_cor = 'AZUL'.
ELSE IF p_verde = 'X'.
  lv_cor = 'VERDE'.
ELSE IF p_roxo = 'X'.
  lv_cor = 'ROXO'.
ENDIF.

But there is a curious and far more elegant way to do the same thing. This is what we call upside down CASE:


CASE 'X'.
  WHEN p_azul.
    lv_cor = 'AZUL'.
  WHEN p_verde.
    lv_cor = 'VERDE'.
  WHEN p_roxo.
    lv_cor = 'ROXO'.
ENDCASE.

How about that?

(Thanks to itspaulkelly for the photo.)

Greetings from Abapinho.

Um comentário a “Upside down CASE”

  1. Leandro Diz:

    Opa! Muito interessante a dica. Nunca tinha pensado nisso.

    Obrigado!

Deixe um comentário


About Abapinho
Abapinho runs on WordPress
Articles (RSS) e Comments (RSS).