Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing nullable(of boolean)

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)?

like image 625
Lukasz Avatar asked Jun 09 '11 19:06

Lukasz


People also ask

What is a nullable 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.

How do I check if a boolean is null or empty in C#?

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 do you check if a nullable variable has 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.

Can a Boolean column be null?

BOOLEAN can have TRUE or FALSE values. BOOLEAN can also have an “unknown” value, which is represented by NULL.


1 Answers

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
like image 151
Alex K. Avatar answered Sep 27 '22 18:09

Alex K.