Access struct element using its string value

eatrader1231231231231233r5235134  

Hello, I just realized that it seems to be possible to access a struct element using its string value:

               struct struct_test
               {  double SL_1;
                  double SL_2;
               }
               LongPos[];
               
               ArrayResize(LongPos,1);

               LongPos[0].SL_1 = 100.0;
               Print(LongPos[0].SL_1);
               
               LongPos[0]."SL_1" = 200.0;
               Print(LongPos[0].SL_1);
               Print(LongPos[0]."SL_1");

Is this a bug or on purpose?

Navdeep Singh  
eatrader1231231231231233r5235134:

Hello, I just realized that it seems to be possible to access a struct element using its string value:

Is this a bug or on purpose?

This is interesting. MQL5 having JS like feature. Maybe it is a new update?

William Roeder  
Navdeep Singh #: This is interesting. MQL5 having JS like feature. Maybe it is a new update?

Fails on MT4.

Navdeep Singh  
William Roeder #:

Fails on MT4.

Yes I did check that immediately.
Fernando Carreiro  
Navdeep Singh #: This is interesting. MQL5 having JS like feature. Maybe it is a new update?
I doubt it would work if using a string variable instead of a string literal. The compiler is probably just ignoring the quotes and nothing else.
Navdeep Singh  
Fernando Carreiro #:
I doubt it would work if using a string variable instead of a string literal. The compiler is probably just ignoring the quotes and nothing else.

Yes It doesn't, So that means it is just a pre-processor thing?

Fernando Carreiro  
Navdeep Singh #: Yes It doesn't, So that means it is just a pre-processor thing?
Probably an overlooked "bug"!
eatrader1231231231231233r5235134  

@Carl Schreiber: I came across this behaviour because of another issue I tried to solve. I have a struct which conatins several information such as open price, different stop losses, open times, closing times etc. I wanted to find the lowest and second lowest stop loss within the struct. Since ArraySort is not applicable to structs I tried the following:

               SL[0,0] = tradeInfo[0].SL_1;
               SL[1,0] = tradeInfo[0].SL_2;
               SL[2,0] = tradeInfo[0].SL_3;
               SL[3,0] = tradeInfo[0].SL_4;
               SL[4,0] = tradeInfo[0].SL_5;

               SL[0,1] = 1;
               SL[1,1] = 2;
               SL[2,1] = 3;
               SL[3,1] = 4;
               SL[4,1] = 5;

               ArraySort(SL);

which returns:


Next, I would like to move the lowest stop loss, which is 2, to the value of 5 thus to the value of 90.98.

Now, my issue appears because I do not know how to bring this information back into my struct. There are so many combinations that I need to nest so many if statements and that sounds really wrong to me ;-)

So my intention was to concatenate the second column of the array (number of the SL) with "SL_"+(string)SL[0,1] to get "SL 2" and use this again to alter the value from the struct using 

string sl="SL_"+(string)SL[0,1];
LongPos[0]."sl" = 90.98;

I have not yet tested it if it works but this brought to my question raised above ;-)

Fernando Carreiro  
eatrader1231231231231233r5235134 #: @Carl Schreiber: I came across this behaviour because of another issue I tried to solve. I have a struct which conatins several information such as open price, different stop losses, open times, closing times etc. I wanted to find the lowest and second lowest stop loss within the struct. Since ArraySort is not applicable to structs I tried the following: which returns: Next, I would like to move the lowest stop loss, which is 2, to the value of 5 thus to the value of 90.98. Now, my issue appears because I do not know how to bring this information back into my struct. There are so many combinations that I need to nest so many if statements and that sounds really wrong to me ;-) So my intention was to concatenate the second column of the array (number of the SL) with "SL_"+(string)SL[0,1] to get "SL 2" and use this again to alter the value from the struct using 

I have not yet tested it if it works but this brought to my question raised above ;-)

I think the issue is that you have a fundamental code logic/design problem. You should rethink how you want to structure your data and/or methods of handling it in a more organised and efficient manner.

Ilyas  
eatrader1231231231231233r5235134:

Hello, I just realized that it seems to be possible to access a struct element using its string value:

Is this a bug or on purpose?

Thank you for message.
This is a bug.
Fixed.

Reason: