Download MetaTrader 5

is there some "StringToEnum" function or an alternative?

To add comments, please log in or register
cyberglassed
613
cyberglassed  

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.

Marco vd Heijden
Moderator
5668
Marco vd Heijden  
MODE_SMMA

is already integer value 2.

Print(IntegerToString(MODE_SMMA));
JD4
1100
JD4  
https://www.mql5.com/en/docs/convert has a large group list of conversion functions.
Documentation on MQL5: Conversion Functions
Documentation on MQL5: Conversion Functions
  • www.mql5.com
Conversion Functions - Reference on algorithmic/automated trading language for MetaTrader 5
cyberglassed
613
cyberglassed  
Marco vd Heijden:

is already integer value 2.

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

Marco vd Heijden
Moderator
5668
Marco vd Heijden  

There is no StringToEnum.

MODE_SMMA

is already integer value 2.

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



int b;

string StringToEnum="MODE_SMMA";

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

Print("b= ",b);



JD4
1100
JD4  
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.
cyberglassed
613
cyberglassed  

thank you guys

Alain Verleyen
Moderator
33501
Alain Verleyen  
cyberglassed
613
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.
JD4
1100
JD4  
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.
cyberglassed
613
cyberglassed  
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.

123
To add comments, please log in or register