Value Types that Might Not Have a Defined Value
Sometimes you work with a value type that does not have a defined value in certain circumstances. For example, a field in a database might need to distinguish between having been assigned a meaningful value and never having been assigned a value. Value types can be extended to take either their normal values or a null value. Such an extension is called a nullable type.
Each nullable type is constructed from the genericstructure. Consider a database that tracks work-related activities. The following example constructs a nullable Boolean type and declares a variable of that type.
Dim ridesBusToWork As Nullable(Of Boolean)
The variable ridesBusToWork can hold a value of True, a value of False, or no value at all. Its initial default value is no value at all, which in this case could mean that the information has not yet been obtained for this person. In contrast, False could mean that the information has been obtained and the person does not ride the bus to work.
You can declare variables and properties with nullable types, and you can declare an array with elements of a nullable type. You can declare procedures with nullable types as parameters, and you can return a nullable type from a Function procedure.
You cannot construct a nullable type on a reference type such as an array, a String, or a class. The underlying type must be a value type. For more information, see.
Using a Nullable Type Variable
The most important members of a nullable type are itsand properties. For a variable of a nullable type, HasValue tells you whether the variable contains a defined value. If HasValue is True, you can read the value from Value. Note that both HasValue and Value are ReadOnly properties.
When you declare a variable with a nullable type, its HasValue property has a default value of False. This means that the variable itself defaults to having no defined value, rather than the default value of its underlying value type. In the following example, the variable numberOfChildren initially has no defined value, even though the default value of the Integer type is 0.
Dim numberOfChildren As Nullable(Of Integer)
A null value is useful to indicate an undefined or unknown value. If numberOfChildren had been declared as Integer, there would be no value that could indicate that the information is not currently available.
You store a value in a variable or property of a nullable type in the normal way. The following example assigns a value to the variable numberOfChildren declared in the preceding example.
numberOfChildren = 2
If a variable or property of a nullable type contains a defined value, you can cause it to revert to its initial state of not having a value assigned. You do this by setting the variable or property to Nothing, as the following example shows.
numberOfChildren = Nothing
Although you can assign Nothing to a variable of a nullable type, you cannot test it for Nothing. You must test its HasValue property for False.
To retrieve the value of a variable of a nullable type, you should first test its HasValue property to confirm that it has a value. If you attempt to read the value when HasValue is False, Visual Basic throws anexception. The following example shows the recommended way to read the variable numberOfChildren of the preceding examples.
If numberOfChildren.HasValue Then MsgBox("There are " & CStr(numberOfChildren) & " children.") Else MsgBox("It is not known how many children there are.") End If
Using Nullable Types with Data
A database is one of the most important places to use nullable types. Not all database objects currently support nullable types, but the designer-generated table adapters do. See "TableAdapter Support for Nullable Types" in.