Nothing and Strings in Visual Basic

The Visual Basic runtime and the .NET Framework evaluate Nothing differently when it comes to strings.

Visual Basic Runtime and the .NET Framework

Consider the following example:

Dim MyString As String = "This is my string"
Dim stringLength As Integer
' Explicitly set the string to Nothing.
MyString = Nothing
' stringLength = 0
stringLength = Len(MyString)
' This line, however, causes an exception to be thrown.
stringLength = MyString.Length

The Visual Basic runtime usually evaluates Nothing as an empty string (""). The .NET Framework does not, however, and throws an exception whenever an attempt is made to perform a string operation on Nothing.

See Also

Introduction to Strings in Visual Basic