enum typecasting

 

Can anyone please explain how typecasting enums works?

I did a typecast of deal type to position type and couldn't figure out why or how it worked. Is there a certain rule to follow while creating the enums?

 
Nelson Wanyama:

Can anyone please explain how typecasting enums works?

I did a typecast of deal type to position type and couldn't figure out why or how it worked. Is there a certain rule to follow while creating the enums?

It is based upon an integer value.  Ideally you always use the enum to avoid confusion but if you have to cast you can also explicitly set the integer value in the enum declaration again to avoid confusion

enum CUSTOM_PERIOD {H1=1, H2=2, H3=3, H4=4, H5=5, H6=6, H7=7, H8=8, H9=9,H10=10, H11=11, H12=12};
 
  1. Nelson Wanyama: Can anyone please explain how typecasting enums works?
    An enum is just an integer with named constants. All you are doing and changing the value to a different type. If the values do not match, typecasting is bogus.
              Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 2020.06.21

  2. Paul Anscombe:
    enum CUSTOM_PERIOD {H1=1, H2=2, H3=3, H4=4, H5=5, H6=6, H7=7, H8=8, H9=9,H10=10, H11=11, H12=12};
    The default is to start at zero and increment by one. Your code can thus be simplified:
    enum CUSTOM_PERIOD {H1=1, H2, H3, H4, H5, H6, H7, H8, H9, H10, H11, H12};

 
William Roeder:
  1. An enum is just an integer with named constants. All you are doing and changing the value to a different type. If the values do not match, typecasting is bogus.
              Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 2020.06.21

  2. The default is to start at zero and increment by one. Your code can thus be simplified:

I hadn't picked up on that, once I override the start I have always numbered them all, thanks

 
William Roeder:
  1. An enum is just an integer with named constants. All you are doing and changing the value to a different type. If the values do not match, typecasting is bogus.
              Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 2020.06.21

  2. The default is to start at zero and increment by one. Your code can thus be simplified:

Thanks William

Reason: