Download MetaTrader 5
To add comments, please log in or register
How to use MQL5.community messages editor? Find out in the article!
dasser
69
dasser 2014.03.20 12:19 

I was slowly Compiling my EA's into build610 worthy EA's.

This part of my code came unstuck where Build590 was fine ( see below switch statement )

The error is : 'MarketInfo' - illegal switch expression '

What I know -

I know MarketInfo returns a double value 0, so the statement "case 0 " may mismatch thinking it is a integer .. - I'm not sure .

Any help appreciated as I know it is something small ...


//+------------------------------------------------------------------+
//|                                               zzzzz-testonly.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//||||||||||||||||||||||__CODE GIVING ERROR ||||||||||||||||||||||||||||||||||||||||||||||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
       Print("test mode =  = " ,MarketInfo(Symbol(),MODE_PROFITCALCMODE)) ;
   switch (MarketInfo(Symbol(),MODE_PROFITCALCMODE))
      {
       case 0:
       {
        Print("case 0 ") ;
       } break;
       case 1:
       {
        Print("case 1" ) ;
       }
      }
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||


 //--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
ydrol
594
ydrol 2014.03.20 12:23  
dasser:

I know MarketInfo returns a double value 0, so the statement "case 0 " may mismatch thinking it is a integer .. - I'm not sure .

Any help appreciated as I know it is something small ...


As you suspected, You could have tried casting it to an int before posting :)

Switch - " Expression of the switch operator must be of integer type."


dasser
69
dasser 2014.03.20 12:33  

I tried this and it worked but I looked up casting and it seems I cannot turn a double into an int.

I just want to understand this before I close out even though I have fixed the issue .. ( hacking it out ) .

      int test = (MarketInfo(Symbol(),MODE_PROFITCALCMODE));
      
      switch(test)

What I understand :

" Each variant of case can be marked with an integer constant, a literal constant or a constant expression"

Ok so this means you cannot use doubles which makes sense ( decimal places issue ) . SOo my expression above seems to have converted a double number to an Integer yet the casting URL says otherwise ..


Any clarification appreciated.

ydrol
594
ydrol 2014.03.20 12:44  

The documentation does not have the greatest wording :) I think the casts indicated by bold arrows can be done implicitly by the compiler and it wont complain as there is no risk of losing some of the number.

Otherwise, I think, you can explicitly cast numbers to any other numeric type you want as long as you are aware you can possibly lose information.In which case some of those arrows should be bi-directional or diagram re-drawn somehow?

/
To add comments, please log in or register