Search code examples
mql4mql5

Switch() Case: with an already defined constant


The compiler keeps whining about the need of a constant for the case type in a switch(){...}. But I have provided a freaking constant. - sorry, /rant mode off

Up in my class I have defined the type plus the TYPE_BULLISH and TYPE_BEARISH constants of the int types. Then I assigned values:

static const int TYPE_BULLISH = 0x001;
static const int TYPE_BEARISH = 0x002;

And I assigned the variable type a value of:

type = TYPE_BULLISH;

Then in the constructor

switch(type) {
    case TYPE_BULLISH: Print("Bullish"); break;
    case TYPE_BEARISH: Print("Bearish"); break;
    default:           Print("Doji");
}

Output error:

'TYPE_BULLISH' - constant expression required

Q1: Any idea what is going on here?

I mean,
Q2: I provided a constant, right?


Solution

  • Try to use #define instead (note: No ; at the end of #define):

    #define TYPE_BULLISH    0x001
    #define TYPE_BEARISH    0x002
    int type = TYPE_BULLISH;
    
    switch(type) {
        case TYPE_BULLISH: Print("Bullish"); break;
        case TYPE_BEARISH: Print("Bearish"); break;
        default:           Print("Doji");
    }