Share via


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.

Public Structure testStruct
    Public name As String
    Public number As Short
End Structure
Dim ts As testStruct, i As Integer, b As Boolean
ts = Nothing 
' The preceding statement sets ts.name to "" and ts.number to 0.
i = Nothing 
b = Nothing 
' The preceding statements set i to 0 and b to False.

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

Dim testObject As Object
testObject = Nothing 
' The preceding statement sets testObject to not refer to any instance.

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

Reference

Dim Statement (Visual Basic)

Concepts

Object Lifetime: How Objects Are Created and Destroyed
Lifetime in Visual Basic