"Not all control paths return a value" error

 

Hello, I am trying to code my system however after compiling it returns the "not all control paths return a value" and when I double click it shows its in the following block, could someone please point out the problem.

Thank you in advance

bool CalculateCurrentSellOrders(string symbol)
  {
   int sells=0;
   for (int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_SELL) sells++;
        }
      }
   if(sells>0) return(true);
  }
 
Siladeh:

Hello, I am trying to code my system however after compiling it returns the "not all control paths return a value" and when I double click it shows its in the following block, could someone please point out the problem.

Thank you in advance


Problem solved


Remove #Property strict

 
Siladeh:

Problem solved


Remove #Property strict


That's a work around, add 1 extra line

   if(sells>0) return(true);
   else     return(false);  // Add this line
 
GumRai:


That's a work around, add 1 extra line


Thank you buddy
 
Or the simpler:
return( sells>0 );
// if(sells>0) return(true);                
// else     return(false);  // Add this line
 

WHRoeder - I keep stripping more lines out of my code thanks to your tips. I would have done exactly the same as GumRai. Thanks!

 
GumRai:


That's a work around, add 1 extra line


Hi GumRai, just for the sake of conversation, you don't really need the else in there, right ? : )

   if(sells>0) return(true);
   return(false); 
 
Siladeh:

Hello, I am trying to code my system however after compiling it returns the "not all control paths return a value" and when I double click it shows its in the following block, could someone please point out the problem.

Thank you in advance

You may get a warning for OrderSelect if you don't check the result.

bool CalculateCurrentSellOrders(string symbol)
  {
   int sells=0;
   for (int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
        Print("OrderSelect() failed with error : "+GetLastError());
      if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MAGICMA)continue;
      if(OrderType()==OP_SELL) sells++;
     }
   if(sells>0) return(true);
   return(false);
  }


Hope it helps

 
thrdel:


Hi GumRai, just for the sake of conversation, you don't really need the else in there, right ? : )


No, you're right.

WHRoeders post above yours is even simpler to achieve the same thing

 
GumRai:


No, you're right.

WHRoeders post above yours is even simpler to achieve the same thing


True, but only for particular situations like this one. The other way is more general, regardless of how many and what conditions are, if none of them return true, it will return false.

It is, must admit, an elegant solution for this particular example.

 
thrdel:

...

and if you don't use :

Hope it helps

You don't need to add -1 as the condition is
i<OrdersTotal()
If you add -1 you miss 1 item of the array.
Reason: