[ABAP] Hilfsklasse für Enumeratoren auf Basis von statischen Objektreferenzen

* Klassenobjekte sind nur statisch verfügbar (CLASS-DATA)
* von ihr kann nicht abgeleitet werden (FINAL)
* mit NEW können keine Objektreferenzen erzeugt werden (CREATE PRIVATE)
CLASS lcl_enum_colors DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
* Konstanten vom Typ des Parameters
    CLASS-DATA: co_red TYPE REF TO lcl_enum_colors READ-ONLY.
    CLASS-DATA: co_green TYPE REF TO lcl_enum_colors READ-ONLY.
    CLASS-DATA: co_blue TYPE REF TO lcl_enum_colors READ-ONLY.

* Klassenkonstruktor
    CLASS-METHODS:
      class_constructor.
ENDCLASS.

CLASS lcl_enum_colors IMPLEMENTATION.
* Vorbelegung der Konstanten
* wird immer beim allerersten Aufruf eines Klassenelementes (Funktion, Variable, Konstante ...) ausgeführt
  METHOD class_constructor.
    co_red   = NEW #( ).
    co_green = NEW #( ).
    co_blue  = NEW #( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
* Variablen vom Typ REF TO lcl_enum_colors
  DATA: lv_color_green TYPE REF TO lcl_enum_colors.
  DATA: lv_color_red TYPE REF TO lcl_enum_colors.
  DATA: lv_color_blue TYPE REF TO lcl_enum_colors.

* Variablen mit konstanten Werten vorbelegen
  lv_color_green = lcl_enum_colors=>co_green.
  lv_color_red = lcl_enum_colors=>co_red.
  lv_color_blue = lcl_enum_colors=>co_blue.

* Werteausgabe
  IF lv_color_green = lcl_enum_colors=>co_green.
    WRITE: / 'Ok.'.
  ELSE.
    WRITE: / 'Not ok.'.
  ENDIF.

  IF lv_color_green = lcl_enum_colors=>co_red.
    WRITE: / 'Ok.'.
  ELSE.
    WRITE: / 'Not ok.'.
  ENDIF.

  IF lcl_enum_colors=>co_red = lcl_enum_colors=>co_blue.
    WRITE: / 'Ok.'.
  ELSE.
    WRITE: / 'Not ok.'.
  ENDIF.

[ABAP] Hilfsklasse für Enumeratoren auf Basis von Named Parameters

CLASS lcl_enum_colors DEFINITION FINAL.
  PUBLIC SECTION.
* Parametertyp
    TYPES: BEGIN OF ty_col_enum,
             col_name TYPE string,
             value    TYPE i,
           END OF ty_col_enum.

* Konstanten vom Typ des Parameters
    CLASS-DATA: co_red TYPE ty_col_enum READ-ONLY.
    CLASS-DATA: co_green TYPE ty_col_enum READ-ONLY.
    CLASS-DATA: co_blue TYPE ty_col_enum READ-ONLY.

* Klassenkonstruktor
    CLASS-METHODS:
      class_constructor.
ENDCLASS.

CLASS lcl_enum_colors IMPLEMENTATION.
* Vorbelegung der Konstanten
* wird immer beim allerersten Aufruf eines Klassenelementes (Funktion, Variable, Konstante ...) ausgeführt
  METHOD class_constructor.
    co_red   = VALUE #( col_name = 'COL_RED'
                        value    = 1 ).

    co_green = VALUE #( col_name = 'COL_GREEN'
                        value    = 2 ).

    co_blue  = VALUE #( col_name = 'COL_BLUE'
                        value    = 3 ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
* Variablen vom Typ Enum
  DATA: lv_color_green TYPE lcl_enum_colors=>ty_col_enum.
  DATA: lv_color_red TYPE lcl_enum_colors=>ty_col_enum.
  DATA: lv_color_blue TYPE lcl_enum_colors=>ty_col_enum.

* Variablen mit konstanten Werten vorbelegen
  lv_color_green = lcl_enum_colors=>co_green.
  lv_color_red = lcl_enum_colors=>co_red.
  lv_color_blue = lcl_enum_colors=>co_blue.

* Werteausgabe
  WRITE: / lv_color_green-col_name, lv_color_green-value.
  WRITE: / lv_color_red-col_name, lv_color_red-value.
  WRITE: / lv_color_blue-col_name, lv_color_blue-value.

* Wertevergleich
  IF lv_color_green = lcl_enum_colors=>co_green.
    WRITE: / 'Ok.'.
  ELSE.
    WRITE: / 'Not ok.'.
  ENDIF.

  IF lv_color_green = lcl_enum_colors=>co_red.
    WRITE: / 'Ok.'.
  ELSE.
    WRITE: / 'Not ok.'.
  ENDIF.

  IF lcl_enum_colors=>co_red = lcl_enum_colors=>co_blue.
    WRITE: / 'Ok.'.
  ELSE.
    WRITE: / 'Not ok.'.
  ENDIF.

[ABAP] Klassenbasierter Enumerator (bis NW 7.50)

Bis NW 7.50 sind beispielsweise folgende Varianten möglich. Ab NW 7.51: Link

Variante 1: Integer

CLASS lcl_enum DEFINITION FINAL.

  PUBLIC SECTION.

    TYPES: ty_enum TYPE i.

    CLASS-DATA: standard TYPE ty_enum VALUE 1 READ-ONLY.
    CLASS-DATA: high TYPE ty_enum VALUE 2 READ-ONLY.
    CLASS-DATA: low TYPE ty_enum VALUE 3 READ-ONLY.

ENDCLASS.

CLASS lcl_enum IMPLEMENTATION.

ENDCLASS.

START-OF-SELECTION.

  DATA: lv_al TYPE lcl_enum=>ty_enum.

  lv_al = lcl_enum=>standard.

  CASE lv_al.
    WHEN lcl_enum=>standard.
      WRITE: / lcl_enum=>standard.
    WHEN lcl_enum=>high.
      WRITE: / lcl_enum=>high.
    WHEN lcl_enum=>low.
      WRITE: / lcl_enum=>low.
  ENDCASE.

Variante 2: String

CLASS lcl_enum DEFINITION FINAL.

  PUBLIC SECTION.

    TYPES: ty_enum TYPE string.

    CLASS-DATA: standard TYPE ty_enum VALUE 'STANDARD' READ-ONLY.
    CLASS-DATA: high TYPE ty_enum VALUE 'HIGH' READ-ONLY.
    CLASS-DATA: low TYPE ty_enum VALUE 'LOW' READ-ONLY.

ENDCLASS.

CLASS lcl_enum IMPLEMENTATION.

ENDCLASS.

START-OF-SELECTION.

  DATA: lv_al TYPE lcl_enum=>ty_enum.

  lv_al = lcl_enum=>low.

  CASE lv_al.
    WHEN lcl_enum=>standard.
      WRITE: / lcl_enum=>standard.
    WHEN lcl_enum=>high.
      WRITE: / lcl_enum=>high.
    WHEN lcl_enum=>low.
      WRITE: / lcl_enum=>low.
  ENDCASE.