Sideway
output.to from Sideway
Draft for Information Only

Content

VB.NET Type of Data
Nullable Value Types
 Using a Nullable Type Variable
  Default Values
  Storing Values
  Retrieving Values
 Comparing Nullable Types
 Propagation
 Using Nullable Types with Data
 See also
  Source/Reference

VB.NET Type of Data

Nullable Value Types

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 have to distinguish between having an assigned value that is meaningful and not having an assigned 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 generic Nullable<T> structure. Consider a database that tracks work-related activities. The following example constructs a nullable Boolean type and declares a variable of that type. You can write the declaration in three ways:

VB
Dim ridesBusToWork1? As Boolean
Dim ridesBusToWork2 As Boolean?
Dim ridesBusToWork3 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 Value Types and Reference Types.

Using a Nullable Type Variable

The most important members of a nullable type are its HasValue and Value 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.

Default Values

When you declare a variable with a nullable type, its HasValue property has a default value of False. This means that by default the variable has no defined value, instead of 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.

VB
Dim numberOfChildren? As 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.

Storing Values

You store a value in a variable or property of a nullable type in the typical way. The following example assigns a value to the variable numberOfChildren declared in the previous example.

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

VB
numberOfChildren = Nothing

Note

Although you can assign Nothing to a variable of a nullable type, you cannot test it for Nothing by using the equal sign. Comparison that uses the equal sign, someVar = Nothing, always evaluates to Nothing. You can test the variable's HasValue property for False, or test by using the Is or IsNot operator.

Retrieving Values

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 try to read the value when HasValue is False, Visual Basic throws an InvalidOperationException exception. The following example shows the recommended way to read the variable numberOfChildren of the previous examples.

VB
If numberOfChildren.HasValue Then
    MsgBox("There are " & CStr(numberOfChildren) & " children.")
Else
    MsgBox("It is not known how many children there are.")
End If

Comparing Nullable Types

When nullable Boolean variables are used in Boolean expressions, the result can be True, False, or Nothing. The following is the truth table for And and Or. Because b1 and b2 now have three possible values, there are nine combinations to evaluate.

b1 b2 b1 And b2 b1 Or b2
Nothing Nothing Nothing Nothing
Nothing True Nothing True
Nothing False False Nothing
True Nothing Nothing True
True True True True
True False False True
False Nothing False Nothing
False True False True
False False False False

When the value of a Boolean variable or expression is Nothing, it is neither true nor false. Consider the following example.

VB
Dim b1? As Boolean
Dim b2? As Boolean
b1 = True
b2 = Nothing

' The following If statement displays "Expression is not true".
If (b1 And b2) Then
    Console.WriteLine("Expression is true")
Else
    Console.WriteLine("Expression is not true")
End If

' The following If statement displays "Expression is not false".
If Not (b1 And b2) Then
    Console.WriteLine("Expression is false")
Else
    Console.WriteLine("Expression is not false")
End If

In this example, b1 And b2 evaluates to Nothing. As a result, the Else clause is executed in each If statement, and the output is as follows:

Expression is not true

Expression is not false

Note

AndAlso and OrElse, which use short-circuit evaluation, must evaluate their second operands when the first evaluates to Nothing.

Propagation

If one or both of the operands of an arithmetic, comparison, shift, or type operation is nullable, the result of the operation is also nullable. If both operands have values that are not Nothing, the operation is performed on the underlying values of the operands, as if neither were a nullable type. In the following example, variables compare1 and sum1 are implicitly typed. If you rest the mouse pointer over them, you will see that the compiler infers nullable types for both of them.

VB
' Variable n is a nullable type, but both m and n have proper values.
Dim m As Integer = 3
Dim n? As Integer = 2

' The comparison evaluated is 3 > 2, but compare1 is inferred to be of 
' type Boolean?.
Dim compare1 = m > n
' The values summed are 3 and 2, but sum1 is inferred to be of type Integer?.
Dim sum1 = m + n

' The following line displays: 3 * 2 * 5 * True
Console.WriteLine($"{m} * {n} * {sum1} * {compare1}")

If one or both operands have a value of Nothing, the result will be Nothing.

VB
' Change the value of n to Nothing.
n = Nothing

Dim compare2 = m > n
Dim sum2 = m + n

' Because the values of n, compare2, and sum2 are all Nothing, the
' following line displays: 3 * <null> * <null> * <null>
Console.WriteLine($"{m} * {If(n, "<null>")} * {If(sum2, "<null>")} * {If(compare2, "<null>")}")

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.

See also

 

Source/Reference


©sideway

ID: 200900020 Last Updated: 9/20/2020 Revision: 0 Ref:

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close

Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Reference 79

Computer

Hardware 249

Software

Application 213

Digitization 32

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 429

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2024 Sideway . All rights reserved Disclaimers last modified on 06 September 2019