Concatenate string with int and print value of variable

 

I have string variables (Candle_1, Candle_2 etc..) which when printed returns "Red" or "Green" from a function.

eg:

When running: Print("**** Candle_1: "+Candle_1);

Output is: **** Candle_1: Red


I would like to print values in a for loop for multiple candles but cant get the value of the variable returned:

            for (int i=1;i<=Candle_Count;i++){

               //string colour = Candle_+i; //option 1

               //string colour = StringConcatenate("Candle_",i); //option 2


               Print("**** colour: "+colour);
            }

I tried both option1 and option 2, neither work

 
twostarii: I have string variables (Candle_1, Candle_2 etc..) which when printed returns "Red" or "Green" from a function. eg: When running: Print("**** Candle_1: "+Candle_1); Output is: **** Candle_1: Red

I would like to print values in a for loop for multiple candles but cant get the value of the variable returned:           

for (int i=1;i<=Candle_Count;i++){
   //string colour = Candle_+i; //option 1
   //string colour = StringConcatenate("Candle_",i); //option 2
   Print("**** colour: "+colour);
}

I tried both option1 and option 2, neither work

  1. Please edit you existing post and use the "</>" icon (or Alt-S) from the posting toolbar to insert your code properly.
  2. MQL is a compiled language and not an interpreted language, so variable names cannot be composed at runtime.
  3. Instead of single variables, use an array. The following code sample is untested/uncompiled:
int    Candle_Count = 5;     // Number of Candles
string Candle[Candle_Count]; // Declare an array of Candle_Count string elements

// Example assignments of text to a string array element;
Candle[0] = "Red";
Candle[1] = "Green";

// Printing out all the Candles
for( int i = 0; i < Candle_Count; i++ )
{
   PrintFormat( "Candle %d Colour: %s", i, Candle[ i ] );
}
Reason: