Download MetaTrader 5
To add comments, please log in or register
Do you often publish source codes? Find out how to do that correctly!
mrwayne
252
mrwayne 2014.04.10 08:28 

https://www.mql5.com/en/articles/1391#3_2


I fix the warning by referring to these articles .
But I don't understand why ( string ) will solve the problem .
Can someone explain the warning " implicit conversion from ' number ' to ' string ' " for me ???

Thanks ~~

Toast
220
Toast 2014.04.10 09:05  

It is telling you that you're trying to fit a square peg into a round hole.

Example:

   int myNumber = 5;
   string myString = myNumber; // you're trying to force a number into a variable designed to store strings

This will give you the above warning: implicit conversion from 'number' to 'string'.

It is telling (warning) you that it has automatically changed the value of myNumber into a string.

By changing the above code as follows:

   int myNumber = 5;
   string myString = (string)myNumber;

You are expressly changing the number 'myNumber' to a string, so it can be stored in the string variable.

This is known as typecasting. There is no need for a warning, as you have expressly commanded the data type change.

mrwayne
252
mrwayne 2014.04.10 09:36  

Thanks for your explanation .
I absorb it all perfectly :)

*Mucks Mucks Mucks*

whroeder1
13596
whroeder1 2014.04.10 12:39  
juniorlcq: Can someone explain the warning " implicit conversion from ' number ' to ' string ' " for me ???
What toast said is correct. Suppose you wanted to write
int SL;
:
void onTick(){
   int pips = 5;
   SL = Bid - pips * pips2dbl;
   :
But somehow the int SL got changed to string SL. Your code is now broken. The new warning is helping you.
Ian Venner
2583
Ian Venner 2014.04.10 15:33  
Tip: Use the string functions, IntegerToString() DoubleToString() TimeToString() etc ... there are several more of them, and use StringConcatonate() to build strings of different values, it does number to string typecasting issues for you.
mrwayne
252
mrwayne 2014.04.12 10:18  
Should I use ( string ) or is IntegerToString() a better choice ?
whroeder1
13596
whroeder1 2014.04.12 11:52  
When there is no real change, I'd use a cast. I'm just telling the compiler that the change is intentional but trivial.
int SecondsElapsed(datetime from, datetime to){
   long seconds = to - from;
   return( (int)seconds );
}
Since there are no 63 year size bars, I know the value fits in an int, even though datetimes require long.
When there is a real change, such as numeric to string, I'd use a function. E.g. datetime to string, do I mean seconds to string (the pre build 600 interpretation) or do I mean a formatted yyyy.mm.dd....
string   PriceToStr(double p){   return( DoubleToStr(p, Digits) );            }
string   DeltaToPips(double d, string s="+"){
                        return( SDoubleToStr(d / pips2dbl, digits_pips, s) ); }
string   DoubleToStrVar(double v, int d=8){                 // 1.60000000 -> 1.6
   string   value = DoubleToStr(v, d);
   for(int iRight = StringLen(value)-1; true; iRight--){
      int      c = StringGetChar(value, iRight);
      if(c != '0'){
         if(c == '.') iRight--;
         break;
      }
   }
   return( StringSubstr(value, 0, iRight+1) );
}
string   SDoubleToStr(double v, int d, string s="+"){       if (v < 0.) s = "";
                                             return( s + DoubleToStr(v, d) ); }
string   SDoubleToStrVar(double v, string s="+", int d=8){  if (v < 0.) s = "";
                                          return( s + DoubleToStrVar(v, d) ); }
from my code
/
To add comments, please log in or register