Download MetaTrader 5

warning - possible loss of data due to type conversion.

To add comments, please log in or register
Publish your article and thousands of people all over the world will read it!
snjage
16
snjage 2016.11.07 18:00 
Someone please help. datetime time=ObjectGet("name",OBJPROP_TIME2); gives a warning - possible loss of data due to type conversion. Why is it so? Thanks in advance 
Marco vd Heijden
Moderator
4707
Marco vd Heijden 2016.11.07 18:03  

This happens when you try to convert between different datatypes the compiler is warning you that there can be dataloss.

Please try:

ObjectGetInteger

The function returns the value of the corresponding object property. The object property must be of the datetime, int, color, bool or char type. There are 2 variants of the function.

1. Immediately returns the property value.

long  ObjectGetInteger(
   long                             chart_id,          // chart identifier
   string                           name,              // object name
   ENUM_OBJECT_PROPERTY_INTEGER     prop_id,           // property identifier
   int                              prop_modifier=0    // property modifier, if required
   );
In stead of ObjectGet.
Fernando Carreiro
2349
Fernando Carreiro 2016.11.07 18:49  
snjage:
Someone please help. datetime time=ObjectGet("name",OBJPROP_TIME2); gives a warning - possible loss of data due to type conversion. Why is it so? Thanks in advance 

The old MT4 function ObjectGet() returns a "double" datatype. However, when getting a different type of data, such as a "datetime", you must typecast it to prevent the warning. So, your code would have to be as follows (both formats are valid):

// either the "C" style
datetime time = (datetime) ObjectGet( "name", OBJPROP_TIME2 );

// or the function style
datetime time = datetime( ObjectGet( "name", OBJPROP_TIME2 ) );

Please note however, that ObjectGet() is an older MT4 function and you should instead use the more modern version ObjectGetInteger() (as mentioned by Marco vd Heijden in the previous post) which is also compatible with MQL5.

You will however, still need to typecast it for a datetime data type, even with the newer function!

ObjectGet - Object Functions - MQL4 Reference
ObjectGet - Object Functions - MQL4 Reference
  • docs.mql4.com
ObjectGet - Object Functions - MQL4 Reference
snjage
16
snjage 2016.11.08 07:06  
Thank you very much Marco vd Heijden and Fernando Carreiro. You have been of great help. 
eevviill6
27
eevviill6 2016.11.09 20:32  
snjage:
Someone please help. datetime time=ObjectGet("name",OBJPROP_TIME2); gives a warning - possible loss of data due to type conversion. Why is it so? Thanks in advance 
datetime time=datetime(ObjectGetInteger(0,"name",OBJPROP_TIME2));
snjage
16
snjage 2016.11.09 21:16  
eevviill6:
datetime time=datetime(ObjectGetInteger(0,"name",OBJPROP_TIME2));
Thank you eevviill6. I'm now enlightened on that 
eevviill6
27
eevviill6 2016.11.09 21:35  
snjage:
Thank you eevviill6. I'm now enlightened on that 
My pleasure.
To add comments, please log in or register