# is there some "StringToEnum" function or an alternative?

613

Hello, if I do:

string a = EnumToString(MODE_SMMA);

I get:

a = "MODE_SMMA"

Now, I would like some function that does just the opposite, I mean something like:

int b = StringToEnum("MODE_SMMA");

b = 2

where 2 is the int value associated to the constant: "MODE_SMMA"

Regards, cyberglassed.

Moderator
5668

`MODE_SMMA`

`Print(IntegerToString(MODE_SMMA));`
1100

https://www.mql5.com/en/docs/convert has a large group list of conversion functions.
Documentation on MQL5: Conversion Functions
• www.mql5.com
613

Marco vd Heijden:

What you say doesn't fit to my needs. As you can read on my question, I know how to convert from enum value to string, but what I wanna do is just the opposite:

with the code:

int b = StringToEnum("MODE_SMMA");

I wanna get the following:

b = 2

Moderator
5668

There is no StringToEnum.

`MODE_SMMA`

```   int b=MODE_SMMA;
Print("b= ",b);```

```int b;

string StringToEnum="MODE_SMMA";

if(StringToEnum=="MODE_SMMA"){b=2;}

Print("b= ",b);```

1100

cyberglassed:

What you say doesn't fit to my needs. As you can read on my question, I know how to convert from enum value to string, but what I wanna do is just the opposite:

with the code:

int b = StringToEnum("MODE_SMMA");

I wanna get the following:

b = 2

You also asked if there was an alternative, which Marco pointed out.
613

thank you guys

Moderator
33501

613

thank you Alain for your advice but actually I haven't a good solution for my problem,
so I have to take some weird workarounds.
It would be really nice that mql5 supports dynamically code evaluation like eval(...) function in PHP and Javascript,
then we could load small pieces of codes from external files (sometimes very interesting) and even implement the function: StringToEnum easily like:

int StringToEnum(string enumId) {
eval("return " + enumId + ";");
}

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

That could be a very interesting advice for the MT5 developer team.

Thank you, Cyberglassed.
1100

cyberglassed:

thank you Alain for your advice but actually I haven't a good solution for my problem,
so I have to take some weird workarounds.
It would be really nice that mql5 supports dynamically code evaluation like eval(...) function in PHP and Javascript,
then we could load small pieces of codes from external files (sometimes very interesting) and even implement the function: StringToEnum easily like:

int StringToEnum(string enumId) {
eval("return " + enumId + ";");
}

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

That could be a very interesting advice for the MT5 developer team.

Thank you, Cyberglassed.
The ability is there already in MQL to load either external libraries (as DLLs) or specific #include files and have it happen this way.  As MQL is also an OOP language, in theory, you could write that function code yourself, and just include it in your programs that you needed that particular functionality on.  Either within the code of your EA, or as an external file.
613

JD4:
The ability is there already in MQL to load either external libraries (as DLLs) or specific #include files and have it happen this way.  As MQL is also an OOP language, in theory, you could write that function code yourself, and just include it in your programs that you needed that particular functionality on.  Either within the code of your EA, or as an external file.

thank you JD4, but unfortunately that doesn't solve my requirements because when you load external DLLs you pass it some parameters and get a result based on that parameters, that's OK, but there you are programming in other language/environment/place and not exactly in mql5. I have programmed custom DLLs and loaded it into mql5 when for me doesn't matter the language that will process my parameters, but on this thread I wanted the data be processed on mql5 so I can make use (for instance) of local and global variables. If you use external DLLs, there you will not know about the values of local and global variables, because it is like an external processing machine. Then, if you have a function like eval(...) available in mql5 (right now is not the case because not implemented) then you can make use of global and local variables, and even functions.