1

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

user3666197
  • 1
  • 6
  • 50
  • 92
  • Do the options in the list have to be in alphabetical order necessarily? – user3722096 Dec 31 '16 at 15:11
  • Someone on another forum gave me a simple solution: enum ENUM_myChoice{ a=1, b=2, c=3, e=4, f=5, g=6 }; Now add "d": enum ENUM_myChoice{ a=1, b=2, c=3, d=7, e=4, f=5, g=6 }; CHOICE = EnumToString(ENUM_myChoice) ; Thank you to all that took the time to reply. I appreciate your willingness to help. – TheRumpledOne Jan 01 '17 at 20:36

1 Answers1

0

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" )
     };
user3666197
  • 1
  • 6
  • 50
  • 92
  • Someone on another forum gave me a simple solution: enum ENUM_myChoice{ a=1, b=2, c=3, e=4, f=5, g=6 }; Now add "d": enum ENUM_myChoice{ a=1, b=2, c=3, d=7, e=4, f=5, g=6 }; CHOICE = EnumToString(ENUM_myChoice) ; Thank you to all that took the time to reply. I appreciate your willingness to help. – TheRumpledOne Jan 01 '17 at 20:37
  • user3666197: I am not sure I understand what you are saying. The solution solved my problem. Yes, I have to put the choices in alphabetical order. But what other choice do I have? Thanks for replying. – TheRumpledOne Jan 02 '17 at 22:34