[ABAP] How do I create and use enums and constants in ABAP?

In ABAP, enums and constants can be created and used as follows:

  1. Enumerations (Enums):

    • Declare an enumeration type using the ENUM statement followed by the name of the enum and the possible values enclosed in parentheses.

    • Example: TYPES: enum_name TYPE ENUM ('Value1', 'Value2', 'Value3').

    • Use the enum type to declare variables and table fields.

    • Example: DATA: enum_var TYPE enum_name.

    • Assign a value to an enum variable using the corresponding enum constant.

    • Example: enum_var = enum_name->Value1.

    • Enums can be used in CASE statements and comparisons.

    • Example:

      1CASE enum_var.
      2  WHEN enum_name->Value1.
      3    " do something
      4  WHEN enum_name->Value2.
      5    " do something else
      6ENDCASE.
      
  2. Constants:

    • Declare a constant using the CONSTANTS statement followed by the name of the constant, the data type, and the value.

    • Example: CONSTANTS c_name TYPE c_type VALUE 'Constant Value'.

    • Use the constant by referring to its name in your code.

    • Example: WRITE: / c_name.

    • Constants can be used in calculations and comparisons.

    • Example:

      1DATA(lv_result) = c_value1 + c_value2.
      2IF lv_result = c_value3.
      3  " do something
      4ENDIF.
      

Note: Enums and constants should be declared at the beginning of the ABAP program or in a global include to ensure their availability throughout the program.