MQL4 expression syntax in IF clause, where I'm doing wrong?

 

Hi, I spent quite a lot of time in trying figuring out what's wrong with this extremely simple lines of code. I'm learning MQL4 language and so far google gave me all the answers, except this.

This short block of code works without any problem: 

      r = order.details.events & OPEN_PRICE_CHANGED;  if ( r != 0 ){

         Alert( Symbol(), "PRICE" );
      } 

order.details.events is and integer, while OPEN_PRICE_CHANGED is a numeric constant defined with the #define keyword.

If I transform the above in the following way (just to make it more readable and easier to maintain in future):

      if ( (order.details.events & OPEN_PRICE_CHANGED) != 0 ){
         Alert( Symbol(), "PRICE" );
      } 

the compiler answers with the following errors:

1- ';' - unexpected token
2- expression not boolean
3- '!=' - unexpected token
4- ')' - unexpected token

I did several tests (many) with all sort of combinations (with or without parentesis, etc.). Maybe the errors changed a little, but nothing interesting.

I also wrote an awful method, just to circumvent the issue:

   bool on(int events, int flag) {
      int result = events & flag;
      return result != 0;
   };

but when I try to use it into the IF clause, like on the following code

      if ( mon.on(order.details.events, OPEN_PRICE_CHANGED) != 0 ){
         Alert( Symbol(), "PRICE" );
      } 

The compiler complains about an unexpected ";", twice (always right before the constant):

';' - unexpected token    TestEA4.mq4    87    41
';' - unexpected token    TestEA4.mq4    87    41

Where I'm wrong? Do I have to use the variable workaround in order to check a flag over a mask of bits?

Thank you all folks!

Davide

 

Since build 600, dot is not allowed in variable name. it is used for classes and structs.

You have to rename  order.details.events

 

Thanks, I never imagined that a variable could contain dots, in my example code order is an object, where details is a field pointing to an other object and events is a public integer variable.

I prefer working with classes, thanks to the latest builds! :)

I noticed that this strange errors appear on rows where defined constants are involved. I'll try to use a final variable (or something similar), to verify my thesis. Unfortunately within the documentation I was not able to find anything related, but I'm a novice!

 

Sorry, dots were widely used in variable names before v.600, so that was the first thing that came to mind. 

In second example, function on() is boolean and you compare it with integer (0). 

You should show more code - how are you variables, classes and members defined.  

 

Unfortunately my release of MetaTrader (build 646) refuses to compile any code containing a defined constant used on an IF expression, like in the following example:

#define MY_CONSTANT 5;

void OnTick() {

   if (MY_CONSTANT == 5) {
      // do something here
   }
}

which returns the following errors:
'5' - unbalanced left parenthesis    TestEA4.mq4    77    8
empty controlled statement found    TestEA4.mq4    77    8
'==' - unexpected token    TestEA4.mq4    77    20
')' - unexpected token    TestEA4.mq4    77    24

where row 77 is the if clause row.

The only workaround, with my poor knowledge, is to use standard variables, the below code, of course, compiles very well:

int MY_CONSTANT = 5;

void OnTick() {

   if (MY_CONSTANT == 5) {
      // something here
   }
}

I'm using the release officially supported by my broker, so in order to say "this is an unsolved bug", I first have to test this combination on a more updated release.

 

#define defines a macro and MY_CONSTANT is then replaced with "5;" through the code.  

#define MY_CONSTANT 5;

void OnTick() {
   // This is what compiler sees:
   if (5; == 5) {
      // do something here
   }
}

 

 Delete ";" in the #define. 

 

wops! it was just right in front of me. I told you, I'm a newbie, but hopefully all the time I lost could help someone else!

thanks!!