Home
VB.NET
IsNot and Is Operators
Updated Sep 18, 2024
Dot Net Perls
Is, IsNot. We use these VB.NET operators to check reference types. With these, we can check reference types against special value such as Nothing.
Some notes. We compare references to Nothing. The "Is" and "IsNot" operators are most often used with the Nothing constant. We can (for example) detect a null string.
Nothing
Example. We see how "IsNot Nothing" and "Is Nothing" are evaluated with a local variable. This pattern of code is useful. It helps if you are not sure the variable is set to something.
Module Module1 Sub Main() Dim value As String = "cat" ' Check if it is NOT Nothing. If value IsNot Nothing Then Console.WriteLine(1) End If ' Change to Nothing. value = Nothing ' Check if it IS Nothing. If value Is Nothing Then Console.WriteLine(2) End If ' This is not reached. If value IsNot Nothing Then Console.WriteLine(3) End If End Sub End Module
1 2
TypeOf. We can use the VB.NET Is-operator with TypeOf. This compares the type of a variable to an existing type. This does not perform casting, but does check the types for equivalence.
TypeOf
Module Module1 Sub Main() Dim value As String = "abc" ' Use Is-operator with TypeOf. If TypeOf value Is String Console.WriteLine("Is String") End If End Sub End Module
Is String
IsNothing. We can use the IsNothing Function instead of using the Is-operator alongside Nothing. This performs the same test, and is slightly shorter.
Note It is possible to use Not IsNothing() as well, which is another way of saying Is Not Nothing.
Module Module1 Sub Main() Dim value As String = Nothing ' Use IsNothing function instead of 2 separate keywords. If IsNothing(value) Then Console.WriteLine("IsNothing") End If End Sub End Module
IsNothing
A review. Reference types are common in VB.NET programs—we create them based on classes. Things like Strings and StringBuilders are reference types.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 18, 2024 (new example).
Home
Changes
© 2007-2025 Sam Allen