warning - possible loss of data due to type conversion.

 
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 
 

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.
 
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
 
Thank you very much Marco vd Heijden and Fernando Carreiro. You have been of great help. 
 
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));
 
eevviill6:
datetime time=datetime(ObjectGetInteger(0,"name",OBJPROP_TIME2));
Thank you eevviill6. I'm now enlightened on that 
 
snjage:
Thank you eevviill6. I'm now enlightened on that 
My pleasure.
 
Fernando Carreiro:

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!

merci

Reason: