|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Nothing (Visual Basic)
Represents the default value of any data type.
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.