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.
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".