I have this code :
StartPosLongitude = (object)time.StartPosition.Position.Long ?? 0
it does not compiles with error :
Cannot implicitly convert type 'object' to 'float'
StartPosLongitude is decladed like this : float StartPosLongitude;
time.StartPosition.Position.Long is returned by a wsdl and is of type float? and is null sometimes.
The error tells me I am trying to convert a object to a float, but that is not what I am doing.
In my opinion am trying to cast a float to an object.
So why does the compiler tells me something else ?
And what would be the proper way to do this, I need to put the value of time.StartPosition.Position.Long into StartPosLongitude but put 0 if time.StartPosition.Position.Long is null.
EDIT time.StartPosition.Position.Long is of type float? not float
I think
StartPosLongitude = (object)time.StartPosition.Position.Long ?? 0
should be
StartPosLongitude = time.StartPosition.Position.Long.HasValue ? time.StartPosition.Position.Long.Value : 0f;
or
StartPosLongitude = time.StartPosition.Position.Long ?? 0f;
because time.StartPosition.Position.Long is type of float? - otherwise
but put 0 if time.StartPosition.Position.Long is null
couldn't happen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With