Enumerations

Enumerations in MQL4 define a custom, limited set of data values using named constants. They introduce a new 4-byte integer-valued data type.

Syntax

enum 
  {
   
  };

  • is a comma-separated list of identifiers (named constants).
  • Value Assignment

  • If a named constant is not explicitly assigned a value, it is assigned automatically. The first member defaults to 0. Subsequent members increment by one from the previous member.
  • Explicit values can be assigned to named constants.
  • Example 1 (Automatic Assignment)

    enum months
      {
       January, // 0
       February, // 1
       March, // 2
       April, // 3
       May, // 4
       June, // 5
       July, // 6
       August, // 7
       September, // 8
       October, // 9
       November, // 10
       December // 11
      };
    

    Example 2 (Explicit and Automatic Assignment)

    enum intervals
      {
       month=1,       // Interval of one month
       two_months,    // Two months (value 2)
       quarter,       // Three months - quarter (value 3)
       halfyear=6,    // Half a year
       year=12        // Year - 12 months
      };
    

    Key Characteristics and Constraints

  • **Size:** The internal representation of any enumerated type is always 4 bytes (e.g., sizeof(months) returns 4).
  • **Naming:** A unique name must always be specified after the enum keyword; anonymous enumerations are not permitted (unlike C++).
  • **Type Safety:** Enumerations enable strict type control for function parameters, as they introduce new named constants.
  • Related Topics

  • [Typecasting](/basis/types/casting)
  • [Bool Type](/basis/types/integer/boolconst)
  • [Real Types (double, float)](/basis/types/double)