2015-12-17 122 views
0

根据调试器,我有一个名为myCancelled的类型为Newtonsoft.Json.Linq.Jtoken的变量,其值为Nothing。我也将它铸造成Object,所有这些情况都失败了。我的问题很简单:如何检查它是Nothing/Null/False/Empty?检查Newtonsoft.Json.Linq.JToken是不是Nothing

这是我试过的。这些条件都不计算为真:

  If myCancelled Is Nothing Then 
       'Doesn't come here 
      End If 
      If myCancelled = DBNull.Value.ToString Then 
       'Doesn't come here 
      End If 
      If myCancelled = "null" Then 
       'Doesn't come here 
      End If 
      If IsDBNull(myCancelled) Then 
       'Doesn't come here 
      End If 
      If myCancelled Is DBNull.Value Then 
       'Doesn't come here 
      End If 
      If String.IsNullOrEmpty(myCancelled) = True Then 
       'Doesn't come here 
      End If 
      If myCancelled.ToString = "Nothing" Then 
       'Runtime error 
      End If 
      If myCancelled = DBNull.Value Then 
       'Runtime error 
      End If 
      If IsNothing(myCancelled) Then 
       'Doesn't come here 
      End If 

我是新来VB.net所以任何指针赞赏。

编辑

这一个工作,但它通过时(myCancelled有其价值,条件是真实的)让误报

  If Not myCancelled Then 
       ' It comes here 
      End If 

回答

1

这是什么工作。 VB很难学习,当你习惯了Java,C#等。

If Not myCancelled.Equals("Y") Then 
    ' It finally came here 
End If 
+0

任何想法为什么“Y”是正确的相等检查什么时候没有什么是实际值? – ErikBrandsma

+0

也许Equals()方法以我想要的方式处理Nothings。事后看来,这可能是一个更好的解决方案:https://stackoverflow.com/questions/378225/how-to-check-for-null-value-in-vb-net – chakeda