Boolean
is a value type, not a reference type. Therefore, the value of a Boolean
variable can never be Nothing
. If you compare a Boolean
to Nothing
, VB.NET first converts Nothing
to the default value for a Boolean
, which is False
, and then compares it to that. Therefore, testing to see if a Boolean
variable Is Nothing
is effectively the same as testing to see if it equals False
. If you need a Boolean
which can be set to Nothing
, you need to make it a Nullable(Of Boolean)
. There is a shortcut for that, though. To make any value type nullable, you can just add a question mark after the type, like this:
Public Function Test(ByVal value As Boolean?)
Return "blabla" + If(value.HasValue, If(value.Value, "1", "0"), "")
End Function
As you'll notice, even with the variable being nullable, you still don't test whether or not it is null by comparing it to Nothing
. It may come as a surprise, but Nullable(Of T)
is actually a value type as well. So, rather than testing to see if it's Nothing
, you should use it's HasValue
property, as I demonstrated in the example.