2

Am new to vb.net and doing a work flow in vb.net, i need to check whether a dictionary is null. I have declared a dictionary, but have not assigned any value to it.

When i use IsNothing() method it gives object reference exception. How can i check?

Dim CustDicAs New Dictionary(Of String, Integer)
CustDic.IsNothing()
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396

1 Answers1

5

You check variables for Nothing with Not Is Nothing or IsNot Nothing or via the old IsNothing function from Visual Basic.

Dim dict As Dictionary(Of String, String)
  1. Not Is Nothing

    If Not dict Is Nothing Then
      ' not nothing 
    End If
    
  2. IsNot Nothing

    If dict IsNot Nothing Then
      ' not nothing 
    End If
    
  3. IsNothing function(VB)

    If Not IsNothing(dict) Then
      ' not nothing 
    End If
    

I wouldn't use the VB6 function IsNothing in .NET anymore since it introduces unneeded dependencies and for the reason mentioned here (it allows value types and always returns False).

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939