[ABAP] Enumerator definieren

Variante 1 (einfaches Beispiel, mit impliziter Aufzählungskonstante 0..n)

TYPES:
  BEGIN OF ENUM e_drive_mode,
    forward,
    backward,
  END OF ENUM e_drive_mode.

DATA: lv_drivemode TYPE e_drive_mode.
lv_drivemode = forward.

DATA(lv_num) = CONV i( lv_drivemode ).

* FORWARD, 0
WRITE: / lv_drivemode, lv_num.

Variante 2 (explizite Aufzählungskonstante mit BASE TYPE)

TYPES:
  BEGIN OF ENUM e_drive_mode BASE TYPE symsgty,
    unknown  VALUE IS INITIAL,
    forward  VALUE 'F',
    backward VALUE 'B',
  END OF ENUM e_drive_mode.

DATA: lv_drivemode TYPE e_drive_mode.
lv_drivemode = forward.

DATA(lv_char) = CONV symsgty( lv_drivemode ).

* FORWARD, F
WRITE: / lv_drivemode, lv_char.

Variante 3 (Klassenbasiert, explizite Aufzählungskonstante mit BASE TYPE)

CLASS lcl_drive_mode DEFINITION FINAL.
  PUBLIC SECTION.
    TYPES: BEGIN OF ENUM e_drive_mode BASE TYPE symsgty,
             unknown  VALUE IS INITIAL,
             forward  VALUE 'F',
             backward VALUE 'B',
           END OF ENUM e_drive_mode.

    CLASS-METHODS:
      check_mode
        IMPORTING
                  i_drive_mode TYPE symsgty
        RETURNING VALUE(rv_ok) TYPE abap_bool.
ENDCLASS.

CLASS lcl_drive_mode IMPLEMENTATION.
  METHOD check_mode.
    rv_ok = abap_false.

    TRY.
* gibt es den übergebenen ENUM-Wert?
        DATA(lv_type) = CONV e_drive_mode( i_drive_mode ).
        rv_ok = abap_true.

      CATCH cx_root INTO DATA(e_txt).
    ENDTRY.

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
* F
  DATA(lv_fwd) = CONV symsgty( lcl_drive_mode=>forward ).
  WRITE: / lv_fwd.

* lcl_drive_mode=>forward
  DATA(lv_type) = CONV lcl_drive_mode=>e_drive_mode( 'F' ).
  WRITE: / lv_type.

* Funktion zum Prüfen von ENUM-Werten
  IF abap_true = lcl_drive_mode=>check_mode( 'X' ).
    WRITE: / 'Enum-Wert vorhanden.'.
  ELSE.
    WRITE: / 'Enum-Wert nicht vorhanden.'.
  ENDIF.

Variante 4 (CL_ABAP_ENUMDESCR)

TYPES: BEGIN OF ENUM e_drive_mode BASE TYPE symsgty,
         unknown  VALUE IS INITIAL,
         forward  VALUE 'F',
         backward VALUE 'B',
       END OF ENUM e_drive_mode.

DATA(lv_mode) = VALUE e_drive_mode( ).

DATA(o_desc) = CAST cl_abap_enumdescr( cl_abap_typedescr=>describe_by_data( lv_mode ) ).

cl_demo_output=>write_data( o_desc->kind ).
cl_demo_output=>write_data( o_desc->type_kind ).
cl_demo_output=>write_data( o_desc->base_type_kind ).
cl_demo_output=>write_data( o_desc->members ).

* HTML-Code vom Demo-Output holen
DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
cl_abap_browser=>show_html( EXPORTING title       = 'ENUM'
                                      html_string = lv_html
                                      container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
WRITE: space.

Variante 5 (bis Release 750)

CONSTANTS: BEGIN OF e_status,
             open      TYPE i VALUE 0,
             close     TYPE i VALUE 1,
             undefined TYPE i VALUE 2,
           END OF e_status.

Links