Compiling-Problem: "implicit conversion from 'string' to 'number'"

 

Hey guys,

I just want to print some rules on the right side of the chart. But there is a problem I don't understand. Why is my string-array automatically converted to a number-array?!

The compliling error occurs in the row containing ObjectSetText().

#property indicator_chart_window
int i, k;
string Rules[] = {"Rule1", "Rule2", "Rule3", "Rule4"};

//--- input parameters
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

   k = 0;
   for(i = 1; i <= ArraySize(Rules)-1; i++)
   {
      if (ObjectFind("TR"+i) == -1)
      {
         ObjectCreate("TR"+i, OBJ_LABEL, 0, 0, 0);
         ObjectSet("TR"+i, OBJPROP_CORNER, 1);
         ObjectSet("TR"+i, OBJPROP_XDISTANCE, 3);
         ObjectSet("TR"+i, OBJPROP_YDISTANCE, k);
         ObjectSetText(Rules[i], 10, "Calibri", White);
         k=k+15;    
      }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   for(i = 1; i <= ArraySize(Rules)-1; i++)
   {
      ObjectDelete("TR"+i);
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

Those are not errors, they are warnings

Your code doesn't do as you want because of this, because it is not accessing array element [0]

 for(i = 1; i <= ArraySize(Rules)-1; i++)

//This would be better

for(i=0; i<ArraySize(Rules); i++)

Also

ObjectSetText(Rules[i], 10, "Calibri", White);

//Should be

ObjectSetText("TR"+(string)i,Rules[i],10,"Calibri",White);
 
   k=0;
   for(i=0; i<ArraySize(Rules); i++)
     {
      if(ObjectFind("TR"+(string)i)==-1)
        {
         ObjectCreate("TR"+(string)i,OBJ_LABEL,0,0,0);
         ObjectSet("TR"+(string)i,OBJPROP_CORNER,1);
         ObjectSet("TR"+(string)i,OBJPROP_XDISTANCE,3);
         ObjectSet("TR"+(string)i,OBJPROP_YDISTANCE,k);
         ObjectSetText("TR"+(string)i,Rules[i],10,"Calibri",White);
         k=k+15;
        }
     }


//in deinit


   
   for(i = 0; i < ArraySize(Rules); i++)
   {
      ObjectDelete("TR"+(string)i);
   }
 

Hello GumRai,

thank you very much for your help! Stupid me... I forgot the name of the object in the ObjectSetText line. I changed the variable i beginning with 0 and the ObjectSetText line.

Could you please tell me why you inserted (string) before i? I tried it without and it also works. I think I read some day that this (string) means an extra definition. But the variable i is defined as integer. So what exactly does (string) before i mean?

Thank you!!

 
mar:

Hello GumRai,

thank you very much for your help! Stupid me... I forgot the name of the object in the ObjectSetText line. I changed the variable i beginning with 0 and the ObjectSetText line.

Could you please tell me why you inserted (string) before i? I tried it without and it also works. I think I read some day that this (string) means an extra definition. But the variable i is defined as integer. So what exactly does (string) before i mean?

Thank you!!


By using (string), you are telling the computer that you are explicitly casting an integer to a string. ie. That is your intention

The warning is just in case that wasn't your intention.

The code will work without it, but I use it so that I don't get 100's of warnings and then have to keep scrolling down through the warnings to see if there are any errors.

Nowadays I prefer to create a string variable "name"

   string name="TR"+(string)i;
   if(ObjectFind(name)==-1)
     {
      ObjectCreate(name,OBJ_LABEL,0,0,0);
      ObjectSet(name,OBJPROP_CORNER,1);
      ObjectSet(name,OBJPROP_XDISTANCE,3);
      ObjectSet(name,OBJPROP_YDISTANCE,k);
      ObjectSetText(name,Rules[i],10,"Calibri",White);
      k=k+15;
     }
 
It is still a little difficult for me to understand why "i" has to be casted to a string. But I will use it from now on to avoid possible errors. I read in a Java documentation about converting datatypes and I was always happy that I don't have to think too much about the large variety of datatypes. New MQL4 changed it...
 
mar:
It is still a little difficult for me to understand why "i" has to be casted to a string. But I will use it from now on to avoid possible errors. I read in a Java documentation about converting datatypes and I was always happy that I don't have to think too much about the large variety of datatypes. New MQL4 changed it...


Actually in this particular case, (string)int is not a true casting, but rather a conversion. If it was a casting, the string would be a subtype of the int, which is not. You have no guarantee how it is performed. Imho better approach is using the IntegerToString() function, which actually does the same, but returns the correct type and is well documented.

The numerous new basic datatypes makes the MQL4 more compatible with C and C++ DLL calls. In your code, you may successfully ignore the new types until you need them in a structure or a parameter.

Reason: