Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there such a thing as a nullable bool in vb.net

I am working through my new MVC book and of course, the samples are all in c# as usual.

There is a line of code that says

public bool? WillAttend { get; set; }

The author explains that the question mark indicates that this is a nullable (tri-state) bool that can be true, false. or null. (A new C# 3 convention.)

Does vb.net support any convention like this. Certainly I can declare a boolean in vb.net and I can explicitly set it to Null (Nothing in vb.net).

What's the difference. IS there more to it in c#. Advantages?

like image 228
Seth Spearman Avatar asked Aug 04 '09 02:08

Seth Spearman


People also ask

Can bool be nullable?

Nullable boolean can be null, or having a value “true” or “false”. Before accessing the value, we should verify if the variable is null or not.

Is null a VB?

The IsNull function returns a Boolean value that indicates whether a specified expression contains no valid data (Null). It returns True if expression is Null; otherwise, it returns False.

Which of the following can be used to test if a nullable variable has data?

Characteristics of Nullable Types The HasValue property returns true if the variable contains a value, or false if it is null. You can only use == and != operators with a nullable type.

What are the nullable types in C#?

The Nullable type is an instance of System. Nullable<T> struct. Here T is a type which contains non-nullable value types like integer type, floating-point type, a boolean type, etc. For example, in nullable of integer type you can store values from -2147483648 to 2147483647, or null value.


3 Answers

You can declare a nullable value 3 ways in VB:

Dim ridesBusToWork1? As Boolean
Dim ridesBusToWork2 As Boolean?
Dim ridesBusToWork3 As Nullable(Of Boolean)

Further Reading: MSDN - Nullable Value Types (Visual Basic).

like image 132
Kredns Avatar answered Oct 19 '22 19:10

Kredns


  1. bool? is just shorthand syntax for a nullable value type: i.e. Nullable<bool>
  2. In VB.NET you can use either one: Boolean? or Nullable(Of Boolean).

You can write it like this with a backing property:

Private _willAttend As Nullable(Of Boolean)
Public Property WillAttend As Nullable(Of Boolean)
    Get
        Return _willAttend
    End Get
    Set(value As Nullable(Of Boolean))
        _willAttend = value
    End Set
End Property

Or just use an auto-implemented property like this:

Public Property WillAttend As Boolean?
like image 20
Joel Coehoorn Avatar answered Oct 19 '22 19:10

Joel Coehoorn


Nullables are available since .NET 2.0. In that version Microsoft implemented Generics (Nullable is a Generic type). Since .NET 3.0 you are able to use the ? in VB.NET too (previously you were only able to use Nullable(of Boolean)).

So as said by Lucas Aardvark in .NET 3.0 you are able to use 3 declarations of nullables, but in .NET 2.0 only 1 being

Dim myBool as Nullable(of Boolean)
like image 44
Gertjan Avatar answered Oct 19 '22 19:10

Gertjan