is there some "StringToEnum" function or an alternative? - page 2

 

i have been following this but i have no clear understanding of what exactly it is that you want to accomplish.

I usually re-code around things by doing them in different ways to end up with the end result i was looking for, things can be done in many ways.

 
Marco vd Heijden:

i have been following this but i have no clear understanding of what exactly it is that you want to accomplish.

I usually re-code around things by doing them in different ways to end up with the end result i was looking for, things can be done in many ways.

thank you Marco. Of course things can be done in many ways (and workarounds), sometimes you have to pick one of them to accomplish your goal, but there are some ways easier than others. In this case the workaround I took to mimic the function: StringToEnum was using multiple string comparisons with "if", but as I said before the eval function (not implemented in mql5) could help to avoid all those if, and also help on maintenance because if constants change their name then you don't have to change the block of ifs.
 

i know exactly what you mean i got hundreds, some times even thousands of if lines in one code block to get to what i need.

you gotta love what you do.

 
cyberglassed:
thank you Marco. Of course things can be done in many ways (and workarounds), sometimes you have to pick one of them to accomplish your goal, but there are some ways easier than others. In this case the workaround I took to mimic the function: StringToEnum was using multiple string comparisons with "if", but as I said before the eval function (not implemented in mql5) could help to avoid all those if, and also help on maintenance because if constants change their name then you don't have to change the block of ifs.

Can you explain please, WHY you need this StringToEnum. Obviously you have a string with an enum value, but why is it on a string ? I am curious to know.

In other words, with the support of eval(...) function we could solve many things at the same time.

That will NEVER happen, don't lost your time with that. Eval() function is an open door to security leak, Metaquotes will not add such function.

 
Marco vd Heijden:

i know exactly what you mean i got hundreds, some times even thousands of if lines in one code block to get to what i need.

you gotta love what you do.

Your problem can NOT be resolved with a StringToEnum(), you already know it (I think).
 
Alain Verleyen:
Your problem can NOT be resolved with a StringToEnum(), you already know it (I think).

Correct but it can be done backwards.

  int b=0;
  
  if(EnumToString(MODE_SMMA)=="MODE_SMMA"){b=2;Print("b= ",b);}

But it would be meaningless since MODE_SMMA already has integer value 2 that is why i wrote that i do not understand what cyberglassed is trying to accomplish.

 
Marco vd Heijden:

Correct but it can be done backwards.

But it would be meaningless since MODE_SMMA already has integer value 2 that is why i wrote that i do not understand what cyberglassed is trying to accomplish.

Marco the workaround I use is something like the following (very minimalist version):

int StringToEnum(string strId) {
        if (false) {}
        else if (strId == "PRICE_CLOSE")     return 1;
        else if (strId == "PRICE_OPEN")      return 2;
        else if (strId == "PRICE_HIGH")      return 3;
        else if (strId == "PRICE_LOW")       return 4;
        else if (strId == "PRICE_MEDIAN")    return 5;
        else if (strId == "PRICE_TYPICAL")   return 6;
        else if (strId == "PRICE_WEIGHTED")  return 7;
        // ...
        return -1;
}

void OnStart() {
        string strId = "PRICE_MEDIAN";
        printf("%s: %d ", strId, StringToEnum(strId));
}

of course I don't start from the constant, I start from an string, I say you this because on your code you wrote the following:

EnumToString(MODE_SMMA)

as if you had from the start the constant itself.

Sorry if I missunderstand you, my english is not too good.

Regards.

 
Alain Verleyen:

Can you explain please, WHY you need this StringToEnum. Obviously you have a string with an enum value, but why is it on a string ? I am curious to know.

In other words, with the support of eval(...) function we could solve many things at the same time.

That will NEVER happen, don't lost your time with that. Eval() function is an open door to security leak, Metaquotes will not add such function.

Hi Alain, I satisfy your curiosity :P

Imagine you are using multiple external mql5 code, then you are dealing with multiple "enum" data types defined on them, and of course, like a human, it is better for you to remember the string name of each value of each "enum". Then on some part you wanna specify some value of some enum in string format, then you can't do it directly, so you have to use some workaround as I wrote above. This have two big disadvantages the first one is you have to collect all values of all enums involved and the second disadvantage is the maintenance, so if you update some external mql5 code where the developer changed the int value associated to some constant representation then you could get unexpected behaviour so you have to keep inspecting for updates on the code.

About the security leak topic... I'm not totally agree with what you said about it is an open door to security leak. Of course it could be an open door, but you as programmer has to set the limits and take care of possible critical situations, I mean you can manage perfectly such situations like in PHP, even on SQL with code injection where you have to parse some critical possible input data from users in case they have access to your code.

Maybe in the future we have little of luck and the developer team can give us some help on this direction, so for now, we can use workarounds :P

 
cyberglassed:

Marco the workaround I use is something like the following (very minimalist version):

of course I don't start from the constant, I start from an string, I say you this because on your code you wrote the following:

as if you had from the start the constant itself.

Sorry if I missunderstand you, my english is not too good.

Regards.

That is why it is called backwards and you start by comparing all enumerations to dissolve the enum value from the string.

int b;
if(EnumToString(MODE_SMA)=="MODE_SMA"){b=0;}
if(EnumToString(MODE_EMA)=="MODE_EMA"){b=1;}
if(EnumToString(MODE_SMMA)=="MODE_SMMA"){b=2;}
if(EnumToString(MODE_LWMA)=="MODE_LWMA"){b=3;}

Print(b);


but isn't that the same thing ?

it does not matter on which side you compare the sting if there is a match it will sign the value to b.

int b;
if("MODE_SMA"==EnumToString(MODE_SMA)){b=0;}
if("MODE_EMA"==EnumToString(MODE_EMA)){b=1;}
if("MODE_SMMA"==EnumToString(MODE_SMMA)){b=2;}
if("MODE_LWMA"==EnumToString(MODE_LWMA)){b=3;}

Print(b);

so here we start with a string value but what is the difference?

None.

 
Marco vd Heijden:

...

but isn't that the same thing ?

it does not matter on which side you compare the sting if there is a match it will sign the value to b.

int b;
if("MODE_SMA"==EnumToString(MODE_SMA)){b=0;}
if("MODE_EMA"==EnumToString(MODE_EMA)){b=1;}
if("MODE_SMMA"==EnumToString(MODE_SMMA)){b=2;}
if("MODE_LWMA"==EnumToString(MODE_LWMA)){b=3;}

Print(b);

so here we start with a string value but what is the difference?

None.

your code above is useless because it will always return "b = 3" (last "if" because you are not using "else if")

if you use "else if", then it will always return "b = 0".

anyway, the info: "b = 3" every time, doesn't give any information.

Reason: