Download MetaTrader 5
To add comments, please log in or register
You can test an application before buying it. Find out how to do that!
Micky
58
Micky 2011.11.15 18:07 
I made a simple test script.
//+------------------------------------------------------------------+
//|                                                    testEnum2.mq5 |
//+------------------------------------------------------------------+
#property script_show_inputs
input ENUM_DAY_OF_WEEK  InpDayOfWeek = SUNDAY; // Day Of Week
string NAME[7] =
  {
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
  };
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
    Print(NAME[int(InpDayOfWeek)]); // Casting and no compile warning.
    int DayOfWeek = InpDayOfWeek;   // No casting and no compile warning. So enum type may be integer.
    Print(NAME[DayOfWeek]);         // No casting and no compile warning.
    Print(NAME[InpDayOfWeek]);      // No casting and no compile warning but compile error! Why?
  }
//+------------------------------------------------------------------+
Error message is:
'InpDayOfWeek' - integer expression expected testEnum2.mq5 24 16

Is enum type integer or not?
Andrey Voytenko
22483
Andrey Voytenko 2011.11.15 23:50  
Is enum type integer or not?

MQL5 compiler controls type conversion - https://www.mql5.com/en/docs/basis/types/casting   

Alternative, you can try this code:

Print(EnumToString(InpDayOfWeek)); 
Documentation on MQL5: Language Basics / Data Types / Typecasting
  • www.mql5.com
Language Basics / Data Types / Typecasting - Documentation on MQL5
Micky
58
Micky 2011.11.16 00:29  
avoitenko:

MQL5 compiler controls type conversion - https://www.mql5.com/en/docs/basis/types/casting   

Alternative, you can try this code:

 

Thanks for your reply, and please excuse my misleading test script.
What I really want to know is whether enum type variable is available for index of array or not.
Andrey Voytenko
22483
Andrey Voytenko 2011.11.16 00:45  

Enum type can be converted to integer type: 

Print(NAME[(int)InpDayOfWeek]);

But imagine that SUNDAY != 0 and MONDAY !=1 ..., and your code will not work.

You can use switch like this:

string DayOfWeekToString(ENUM_DAY_OF_WEEK day)
{
   switch(day)
   {
      case SUNDAY: return("Sunday");
      case MONDAY: return("Monday");

      ...

      default:return("");
   }
}

 

 

Micky
58
Micky 2011.11.16 01:26  
I am very thankful to you for your advice.

Anyway, the problem is not the content, but the form. 
My test code is only for example.
It could be compiled with previous build 5XX, but it can not be compiled with current build 540.
So, what I really want to know is whether this compile error is degradation or not.

Thank you.
Alexey Da
Moderator
6579
Alexey Da 2011.11.16 11:17  

Thanks a lot. It was fixed.

At this moment you better use casting like this

Print(NAME[(int)InpDayOfWeek]);

 

Micky
58
Micky 2011.11.16 14:48  
alexvd:

Thanks a lot. It was fixed.

At this moment you better use casting like this

 

I see. I don't check in the casting codes in my trunk of repository, and I will be waiting for the fixed build.

Thank you.

/
To add comments, please log in or register