Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Integer and Integer? results in Boolean? not Boolean

I'm just writing a bit of code to compare an id of integer with an id of integer? for example:

Dim id As Integer = 1
Dim nullId As Integer? = Nothing
Dim areEqual As Boolean
areEqual = nullId = id

When I try to compile the code I get a compiler error:

Option Strict On disallows implicit conversions from 'Boolean?' to 'Boolean'.

Whilst it's very simple for me to work around this, I was hoping that someone might be able to explain what's going in the compiler to give this warning.

like image 840
ilivewithian Avatar asked Dec 29 '22 23:12

ilivewithian


1 Answers

It's one of the quirks of nullable types. NULL (in general terms) means "don't know". Comparing known with unknown results in unknown (because you don't know if they're the same).

It's the same with nullable types in .NET. Comparing an Integer? with an Integer results in a Boolean?, because you might get True, False or "don't know".

like image 166
Roger Lipscombe Avatar answered Jan 07 '23 12:01

Roger Lipscombe