Download MetaTrader 5

I found BUG in Nested if - else if - else if - else. I can't declare same variable name in other blocks

To add comments, please log in or register
Interested in earning money via MQL5 Cloud Network? Enter your profile!
Musngi
301
Musngi 2016.10.14 19:31 

I found BUG in Nested if - else if - else if - else. I can't declare same variable name in other blocks. 

According to MQL4 documentation: A variable declared inside a block (part of code enclosed in curly brackets) belongs to the local scope. Such a variable is not visible (and therefore not available) outside the block, in which it is declared. Since the scope of a local variable is the block in which it is declared, it is possible to declare variables with the same name, as those of variables declared in other blocks; as well as of those declared at upper levels, up to the global level.

 for(int c=0; c<=5; c++)
   {
      if(Dict_JPY[c] < 0 && x[c] == False)      
      {
         double neg_b = Dict_JPY[c];
         ObjectSetText("Result_JPY"+c, -neg_b, 8, "Verdana", Blue);
      }
      else if(Dict_JPY[c] > 0 && x[c] == True)   
      {
         double pos_b = Dict_JPY[c];
         ObjectSetText("Result_JPY"+c, -pos_b, 8, "Verdana", Blue);
      } 
      else if(Dict_JPY[c] < 0)
      {
         double neg_b = Dict_JPY[c];
         ObjectSetText("Result_JPY"+c, -neg_b, 8, "Verdana", Green);
      }
      else
      {
         double pos_b = Dict_JPY[c];
         ObjectSetText("Result_JPY"+c, -pos_b, 8, "Verdana", Red);
      }
   }


Description: 

'neg_b' - variable already defined
'pos_b' - variable already defined

2 error(s)
Keith Watford
Moderator
9624
Keith Watford 2016.10.14 21:12  

If you don't use

#property strict

then the compiler will give these errors.

It's not a bug, without #property strict it allows the compiler to compile code written with earlier versions.

I believe that you should always use #property strict with new code.

To add comments, please log in or register