Switch command syntax?
This link will show you the switch syntax : https://www.mql5.com/en/docs/basis/operators/switch, I think your problem is :
Switch Operator
Compares the expression value with constants in all the case variants and passes control to the operator that corresponds to the expression value. Each variant of case can be marked with an integer constant,
a literal constant or a constant expression. The constant expression
can't contain variables or function calls. Expression of the switch operator must be of integer type.
You can't use string type for switch operator.
- www.mql5.com
As said above, switch expression must be integer. (kind of strange that the example uses string). Just use ifs
ENUM_TIMEFRAMES SetPeriodName (string period){Where is this string coming from? Instead, make it an input, no conversion required.
input ENUM_TIMEFRAMES inpPeriod=PERIOD_CURRENT; ENUM_TIMEFRAMES period; // Actual ⋮ int OnInit(){ period = inpPeriod == PERIOD_CURRENT ? ENUM_TIMEFRAMES(_Period) : period;
Sorry being so fool, but I cannot find switch command syntax in https://www.mql5.com/en/docs. When I write code
ENUM_TIMEFRAMES SetPeriodName (string period){
switch(period)
{
case "M1" : return(PERIOD_M1);
case "M2" : return(PERIOD_M2);
...
case "Daily" : return(PERIOD_D1);
case "Weekly" : return(PERIOD_W1);
case "Monthly": return(PERIOD_MN1);
default : Print(" cannot set period")
}
}
compiler shows error 'period' illegal switch expression type
I don't belive my eyes. Is it indeed restricted using string variables in switch cases or is it something else? I want to convert string values to corresponding timeframe values.