Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to copy a value from float? to float, and put zero if the float? is null?

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

like image 928
GuidoG Avatar asked Jan 29 '26 02:01

GuidoG


1 Answers

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.

like image 71
fubo Avatar answered Feb 03 '26 07:02

fubo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!