Nothing (Visual Basic)

Represents the default value of any data type.

Remarks

Assigning Nothing to a variable sets it to the default value for its declared type. If that type contains variable members, they are all set to their default values. The following example illustrates this for scalar types.

Module Module1
    Public Structure testStruct
        Public name As String 
        Public number As Short 
    End Structure 

    Sub Main()

        Dim ts As testStruct
        Dim i As Integer 
        Dim b As Boolean 

        ' The following statement sets ts.name to Nothing and ts.number to 0.
        ts = Nothing 

        ' The following statements set i to 0 and b to False.
        i = Nothing
        b = Nothing

        Console.WriteLine("ts.name: " & ts.name)
        Console.WriteLine("ts.number: " & ts.number)
        Console.WriteLine("i: " & i)
        Console.WriteLine("b: " & b)

    End Sub 

End Module

If the variable is of a reference type, a value of Nothing means that the variable is not associated with any object. The variable has a null value. The following example demonstrates this.

Module Module1

    Sub Main()

        Dim testObject As Object 
        ' The following statement sets testObject so that it does not refer to  
        ' any instance.
        testObject = Nothing 

        Dim tc As New TestClass
        tc = Nothing 
        ' The fields of tc cannot be accessed. The following statement causes  
        ' a NullReferenceException at run time. (Compare to the assignment of 
        ' Nothing to structure ts in the previous example.) 
        'Console.WriteLine(tc.field1) 

    End Sub 

    Class TestClass
        Public field1 As Integer 
        ' . . . 
    End Class 
End Module

To test reference and nullable type variables for Nothing values, use the Is operator or the IsNot operator. Comparisons that use the equal sign, for example, someVar = Nothing, always evaluate to Nothing. The following example shows comparisons that use the Is and IsNot operators.

Module Module1
    Sub Main()

        Dim testObject As Object
        testObject = Nothing 
        ' The following statement displays "True".
        Console.WriteLine(testObject Is Nothing)

        Dim tc As New TestClass
        tc = Nothing 
        ' The following statement displays "False".
        Console.WriteLine(tc IsNot Nothing)

        Dim n? As Integer 
        ' The following statement displays "True".
        Console.WriteLine(n Is Nothing)
        n = 4
        ' The following statement displays "False".
        Console.WriteLine(n Is Nothing)
        n = Nothing 
        ' The following statement displays "False".
        Console.WriteLine(n IsNot Nothing)

    End Sub 

    Class TestClass
        Public field1 As Integer 
        Dim field2 As Boolean 
    End Class 
End Module

For more information and examples, see Nullable Value Types.

When you assign Nothing to an object variable, it no longer refers to any object instance. If the variable had previously referred to an instance, setting it to Nothing does not terminate the instance itself. The instance is terminated, and the memory and system resources associated with it are released, only after the garbage collector (GC) detects that there are no active references remaining.

See Also

Concepts

Object Lifetime: How Objects Are Created and Destroyed

Lifetime in Visual Basic

Nullable Value Types

Reference

Dim Statement (Visual Basic)

Is Operator (Visual Basic)

IsNot Operator

Change History

Date

History

Reason

August 2008

Updated to include information about nullable value types.

Information enhancement.

August 2008

Added examples that use Is and IsNot operators to check for Nothing values.

Customer feedback.