简体   繁体   中英

How to check dictionary is null in VB.NET?

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()

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM