14 out of 60 rated this helpful - Rate this topic

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.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Default value of String is NOT an empty string

The issue below is fixed in this same topic for later versions of Visual Studio.

The example in this entry is incorrect. It says 'The preceding statement [Dim ts As testStruct] sets ts.name to ""...'. This is wrong; creating a new instance of a value type sets all of its members to their default value; and since String is a reference type, its default value is Nothing. While its true that testing the equality of ts.name and "" will return true, this is a peculiarity of VB.NET, since VB treats empty strings as equivalent; but if you were to call ts.name.Length, for example, you would get a NullReferenceException, since ts.name is set to Nothing, not a string instance.

See the bug report (improperly closed as "By Design") at https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=249907 for further discussion.