Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

When testing against Nothing, you need to use the Is keyword, but you won't be able to pass your value as a Boolean since it only works on reference types.

If( value Is Nothing, "", If( value, "1", "0" ) )

It is also worth noting that Nothing defaults to the default value if it is not a reference type.

You can change your signature to expect an Object rather than a Boolean if you want to do this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top