MT4 Enumeration

2.5k views Asked by At

I am using the MT4 enumeration for a selection input:

enum ENUM_myChoice{ 
     a, b, c, e, f, g
     };

The problem is if I have to add "d" to the list in alphabetical order,
all of my templates using e, f or g are ruined because they are off by 1.

Is there an elegant solution to this or only brute force?

Thanks in advance

1

There are 1 answers

2
user3666197 On

How the MQL4 enum anEnumNAME{...}; syntax works?

Well,
given you need to preserve
both the "old" enum ( the one without "d" ) ordering
and the alphabetical-order too
only one thing is sure - you cannot use enum as proposed above for that.

MQL4 implements enum as a syntax sugar for registering "named constants" for the compiler phase, and translates such elements into an ordered enumeration of a new, specific data-type ( not matching other, strong-typed language data-types in compilation phase ).

Rule: If a certain value is not assigned to a named constant that is a member of the enumeration, its new value will be formed automatically. If it is the first member of the enumeration, the 0 value will be assigned to it. For all subsequent members, values will be calculated based on the value of the previous members by adding one.

This means, you cannot mix and mangle alphabet ordering and incidentally arranged sequential ordering ( as Hollywood states in trailing titles: "In the order of appearance" ).


So what can one do?

A Rainman solution ( "Go to Holbrook, with Charlie Babbit." ):

enum ENUM_myChoice{ 
     a = uchar( "a" ),
     b = uchar( "b" ),
     c = uchar( "c" ),
     e = uchar( "e" ),
     f = uchar( "f" ),
     g = uchar( "g" ),
  // ----------------- WHERE ONE CAN BENEFIT AN OUT-OF-ORDINAL ORDER ADDING:
     d = uchar( "d" )
     };