Download MetaTrader 5

StringToEnum

To add comments, please log in or register
Do you want to order an application? Find out how to obtain the desired result
Henrique Vilela
1235
Henrique Vilela 2015.07.08 01:11 

Hi everybody,

It's possible to convert an enum to string though EnumToString.

What about the reverse operation?

There's an way to convert it back (other than create and function and compare item by item)?

Alain Verleyen
Moderator
30752
Alain Verleyen 2015.07.08 17:13  
Henrique Vilela:

Hi everybody,

It's possible to convert an enum to string though EnumToString.

What about the reverse operation?

There's an way to convert it back (other than create and function and compare item by item)?

I can't imagine why you need such function.
JD4
1101
JD4 2015.07.08 20:37  
Sounds like a for/next loop would work, and put the values into an array of string objects.  As far as why, I guess the same reason for doing lots of things, why not?  Many things are possible now because someone at one point thought of "why can't we do this?" or something similar.
Henrique Vilela
1235
Henrique Vilela 2015.07.08 20:43  
JD4:
Sounds like a for/next loop would work, and put the values into an array of string objects.  As far as why, I guess the same reason for doing lots of things, why not?  Many things are possible now because someone at one point thought of "why can't we do this?" or something similar.

I totally agree with you, the reason doesn't matter at all. :)

But I guess there's no ready-to-use function for this, right? I guess a "switch" statement or the array of strings is the way to go.

Alain Verleyen
Moderator
30752
Alain Verleyen 2015.07.08 20:52  
Henrique Vilela:

I totally agree with you, the reason doesn't matter at all. :)

But I guess there's no ready-to-use function for this, right? I guess a "switch" statement or the array of strings is the way to go.

The reason matter if you need my advice.

Alain Verleyen
Moderator
30752
Alain Verleyen 2015.07.08 20:54  
JD4:
Sounds like a for/next loop would work, and put the values into an array of string objects.  As far as why, I guess the same reason for doing lots of things, why not?  Many things are possible now because someone at one point thought of "why can't we do this?" or something similar.
I asked "why" as in my opinion such need can only result from bad coding practice. Unless I missed something, in which case I hope to learn something.
JD4
1101
JD4 2015.07.08 21:04  
Henrique Vilela:

I totally agree with you, the reason doesn't matter at all. :)

But I guess there's no ready-to-use function for this, right? I guess a "switch" statement or the array of strings is the way to go.

@ Alain

It wasn't meant like that at all.  And in fact, sometimes there is really no good reason to do something, even in coding.  Someone might just want to see if it can be done a certain way, and who knows, it might end up leading off in a totally new direction, because someone else sees it, and thinks "well maybe if we do it this way instead..." and so on.  Stranger things have happened.  But you are right, usually there is a reason to code something a certain way.

Back to Henrique:

I do not think a switch would be the ideal solution, and here's why.  If you will always just use the exact same enum'd list, yes, this would work.  However, if you want your code to be adaptable, without having to re-write it every time the list changes, then I think the for/next would be the way to go.  You can code it so it gets the next chunk (separated by something) and write it to string (or possibly back to enum from a string array), and all you would need to do is give the loop the number of items you want to convert.  For other things, this would be adaptable as well, to my mind anyway, with minimal coding changes.

Edit: I just thought of something that might make this even more wide range adaptable, and not totally sure about how to code it, just have a grasp of the concept.  I know in the OOP language I learned, Java, you can overload functions (make multiple functions with the same name that accept different inputs), but maybe an adaptable version of this idea could work here.  Make a function, either within an object or on it's own within the program that takes as inputs, the from, the to, and the number for one side of the function, and on the other side, the exact opposite.  Like the from enum to string for one, and the string to enum on the other. 

Marco vd Heijden
Moderator
4706
Marco vd Heijden 2015.07.08 21:39  
Alain Verleyen:
I asked "why" as in my opinion such need can only result from bad coding practice. Unless I missed something, in which case I hope to learn something.

Well to be honest i did struggle with this issue for some time.

As you know the input panel is static, this results in the following, suppose i want to have a broad as possible audience, my EA needs to work with as much currency pairs as possible.

Due to the fact it is static, means that many,many traders using my EA will see a lot of currency pairs in the input settings, that are actually not available on their broker feed, which can lead to confusion.

Had it been possible to StringToEnum() the input settings symbol list in a way that it would only present the available symbols, it would have made a rather large difference when it comes to flexibility and comfort in usage.

This is about the only thing i never got to work properly, i found a solution for every other problem so far, but not the possibility to synchronize the symbol list in input settings with Market watch (1) i just had to accept it was not possible.

Alain Verleyen
Moderator
30752
Alain Verleyen 2015.07.08 22:36  
Marco vd Heijden:

Well to be honest i did struggle with this issue for some time.

As you know the input panel is static, this results in the following, suppose i want to have a broad as possible audience, my EA needs to work with as much currency pairs as possible.

Due to the fact it is static, means that many,many traders using my EA will see a lot of currency pairs in the input settings, that are actually not available on their broker feed, which can lead to confusion.

Had it been possible to StringToEnum() the input settings symbol list in a way that it would only present the available symbols, it would have made a rather large difference when it comes to flexibility and comfort in usage.

This is about the only thing i never got to work properly, i found a solution for every other problem so far, but not the possibility to synchronize the symbol list in input settings with Market watch (1) i just had to accept it was not possible.

You want the users of your EA to choose the symbols the EA has to manage, right ? How a StringToEnum() can help ?

Please give more information about your issue.

Carl Schreiber
6609
Carl Schreiber 2015.07.08 22:55  
Not tested but try:
#define EnumToString(e) (string)((int)(e))
and reverse you have to use StringToInteger().
JD4
1101
JD4 2015.07.08 23:04  
calli:
Not tested but try:
#define EnumToString(e) (string)((int)(e))
and reverse you have to use StringToInteger().
Not sure the StringToInteger would work here, unless you are using a specific value and then re-coding the integers to what the enum value would be, like 1=NORTH, 2=SOUTH, etc.  The original concept is to be able to convert an enumerated list of values, and then take each specific item, and convert it to a string.  For example, have an enumerated list of trading pairs, EURUSD, GBPCAD, and so on, and then somehow convert to string, presumably for an output to show "This will trade the following pairs:  EURUSD, GBPCAD...".  That is how I am reading the question, at least.
123
To add comments, please log in or register