Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vb.net Boolean and Nothing

Let's define this function :

Public Function Test(ByVal value As Boolean)
    Return "blabla" + If(value = Nothing, "", If(value, "1", "0"))
End Function

I want it to do the following : Test(True) -> "blabla1", Test(False) -> "blabla0", Test(Nothing) -> "blabla".

Problem is that Test(Nothing) returns "blabla0".

like image 249
Arthur Rey Avatar asked Nov 12 '13 14:11

Arthur Rey


People also ask

What is null in Visual Basic?

In Visual Basic 6.0, the Null keyword indicated that a field contained no valid data, and the IsNull function was used to test for Null. In addition, Visual Basic 6 supported Null propagation when Null was used in an expression, the result of the expression would also be Null.

What is a Boolean expression in VB?

A Boolean expression is an expression that evaluates to a value of the Boolean Data Type: True or False . Boolean expressions can take several forms. The simplest is the direct comparison of the value of a Boolean variable to a Boolean literal, as shown in the following example.

How do you set a Boolean variable to be false?

To declare a Boolean variable, we use the keyword bool. To initialize or assign a true or false value to a Boolean variable, we use the keywords true and false. Boolean values are not actually stored in Boolean variables as the words “true” or “false”.

Is Boolean a numeric data type?

Indeed, a Boolean variable may be regarded (and implemented) as a numerical variable with one binary digit (bit), or as a bit string of length one, which can store only two values.


2 Answers

A Boolean value can never be null (Nothing), the values that are possible are True and False. You need a nullable value, a Boolean?, for it to be able to be null.

Use the HasValue and Value properties of the nullable value to check if there is a value, and get the value:

Public Function Test(ByVal value As Boolean?)
  Return "blabla" + If(Not value.HasValue, "", If(value.Value, "1", "0"))
End Function
like image 148
Guffa Avatar answered Sep 17 '22 20:09

Guffa


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.

like image 32
Steven Doggart Avatar answered Sep 16 '22 20:09

Steven Doggart