Nothing, IsNothing (Null)
This page was last reviewed on Nov 17, 2022.
Dot Net Perls
Nothing. An object may not exist. A reference may point to nothing. We use the literal Nothing to indicate a null, nil, nonexistent object.
Nothing details. We assign to Nothing, test against it, and ponder nothingness. A NullReferenceException is caused by accessing something on a Nothing value.
First example. This program assigns a String variable to Nothing. This is not an empty string. It is a String object that does not exist. We compare it to Nothing in an If-statement.
Part 1 We use the "Is" operator for testing references against Nothing. Using "=" can give incorrect results.
Part 2 We use the IsNothing function to see if a reference is equal to Nothing. This is another syntax form for the same thing.
Module Module1 Sub Main() ' This reference equals Nothing. Dim s As String = Nothing ' Part 1: directly test against Nothing. If s Is Nothing Then Console.WriteLine("1") End If ' Part 2: use the IsNothing function. If IsNothing(s) Then Console.WriteLine("2") End If End Sub End Module
1 2
NullReferenceException. This exception (not NothingReferenceException) is provoked by accessing a member on Nothing. If a reference may be Nothing, we must test before using its members.
Module Module1 Sub Main() ' Assign a String to Nothing. Dim s As String = Nothing ' This causes a NullReferenceException. Dim len As Integer = s.Length ' Not reached. Console.WriteLine(len) End Sub End Module
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
Module. When a Module or Class is created, the fields within it are automatically set to Nothing. You never need to assign them to Nothing at creation time.
Tip Variables in VB.NET never have garbage values. In managed code, like the .NET Framework, initial values are 0 or Nothing.
Imports System.Text Module Module1 ''' <summary> ''' A module-level field. ''' </summary> Dim s As StringBuilder Sub Main() ' The field is nothing when the program begins. If IsNothing(s) Then Console.WriteLine(True) End If End Sub End Module
Values. Nothing can be used with value types such as Integers. When used on values, "Nothing" indicates the default value for the type. So an Integer assigned to Nothing is zero.
Tip Structures are too a value type. When we assign them to Nothing, we get an empty Structure (with no bits set to 1).
Module Module1 Sub Main() ' Initialize an Integer to Nothing. Dim size As Integer = Nothing ' It equals zero. Console.WriteLine(size) If size = 0 Then Console.WriteLine("Size is 0") End If If size = Nothing Then Console.WriteLine("Size is nothing") End If End Sub End Module
0 Size is 0 Size is nothing
String.IsNullOrEmpty. A string in VB.NET can be assigned to Nothing, which is the same concept as Null. We can test a Nothing String with the String.IsNullOrEmpty method.
Tip The term "null" is simply a different name for "Nothing." There is no "IsNothingOrEmpty" string method.
Module Module1 Sub Main() Dim value As String = Nothing ' Nothing is the same as "Null." If String.IsNullOrEmpty(value) Then Console.WriteLine("NULL OR EMPTY") End If End Sub End Module
Null versus nothing. Nothing is not just like null in C#. Instead, it has a special behavior when used with value types—it acts like the default.
Is, IsNot. References can be compared with the "Is" and "IsNot" operators. In VB.NET, we use the Is Nothing and IsNot Nothing expressions to test class references.
Nullable. Value types cannot be equal to null. But with the Nullable structure, we can wrap an Integer and set it to null (with Nothing). This can help programs be clearer.
A summary. Nothing has two meanings. With references, it means a reference that points to no object. And with values, it means the default (zero-initialized) value.
On references, Nothing is null. It may provoke a NullReferenceException. As VB.NET developers, we must understand that "null" refers to Nothing. The terms are confusing.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 17, 2022 (simplify).
© 2007-2024 Sam Allen.