I'm trying to compare two variables of type nullable(of boolean)
in VB.NET 2010. One of the variables has a value False and the other is Nothing. Now I was expecting the following expression to evaluate to true, but this is not the case:
Dim var1 as nullable(of boolean) = False
Dim var2 as nullable(of boolean)
var2 = Nothing
If var1 <> var2 Then
msgbox "they are different"
End If
Why don't I see my MsgBox? How should I compare two nullables (of boolean)?
You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false . However, in some applications a variable value can be undefined or missing.
In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value.
How to access the value of Nullable type variables? You cannot directly access the value of the Nullable type. You have to use GetValueOrDefault() method to get an original assigned value if it is not null. You will get the default value if it is null.
BOOLEAN can have TRUE or FALSE values. BOOLEAN can also have an “unknown” value, which is represented by NULL.
You can use Nullable.Equals
:
Indicates whether two specified
Nullable(Of T)
objects are equal.
If Not Nullable.Equals(var1, var2) Then
MsgBox("they are different")
End If
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