Switch - declare variable

 

Hi,

how do I declare variable x in this code:

switch(x)
  {
   case 'A':
      Print("CASE A");
      break;
   case 'B':
   case 'C':
      Print("CASE B or C");
      break;
   default:
      Print("NOT A, B or C");
      break;
}

int or string is wrong. Compiler return error. Variable x will be value one char A, B, C, D, etc.

Thank zou.

 

I don't believe you can use switch with a string.

From the documentation, "Expression of the switch operator must be of integer type."

 

ok, I know it. But how declare variable x?

int x; // ok, but x = number..

string x; // wrong

Can you write declare variable x, please? (x= one char value, at this sample up

 

Like this . . . .

int x;

switch(x)
  {
   case 1:
      Print("CASE A");
      break;
   case 2:
   case 3:
      Print("CASE B or C");
      break;
   default:
      Print("NOT A, B or C");
      break;
   }
 

;-) but in the sample is for x as number and I would like code for x (char) case 'A':

x as number - I understand,

x as char 'A' (sample code from documentation) - don´t understand

 
endy5:

;-) but in the sample is for x as number and I would like code for x (char) case 'A':

x as number - I understand,

x as char 'A' (sample code from documentation) - don´t understand

All you need to understand is what is written in the documentation . . . "Expression of the switch operator must be of integer type." the example is unfortunate and misleading. x can't be a string . . .
 
endy5:

;-) but in the sample is for x as number and I would like code for x (char) case 'A':

x as number - I understand,

x as char 'A' (sample code from documentation) - don´t understand

All you need to understand is what is written in the documentation . . . "Expression of the switch operator must be of integer type."

I would agree that the example is unfortunate and misleading. x can't be a string . . .
 

ok, thank Raptor!

Do you exist some function MQL which return ASCII code char?

ASCII number I use as variable x in switch. It is good idea?

 
You can use StringGetChar() to return the ASCII code for a character in a string . . . I can't say if it's a good or bad idea without understanding what you are trying to achieve.
 
Thank you very mutch, Raptor! It is all.
Reason: