我想做的就是检查一个对象是否为空,但无论我做什么,如果它编译,它就会抛出一个 NullReferenceException
只是试图检查!这是我所做的:
If ((Not (comp.Container Is Nothing)) And (Not (comp.Container.Components Is Nothing))) Then
For i As Integer = 0 To comp.Container.Components.Count() - 1 Step 1
fixUIIn(comp.Container.Components.Item(i), style)
Next
End If
If ((Not IsDBNull(comp.Container)) And (Not IsDBNull(comp.Container.Components))) Then
For i As Integer = 0 To comp.Container.Components.Count() - 1 Step 1
fixUIIn(comp.Container.Components.Item(i), style)
Next
End If
If ((Not IsNothing(comp.Container)) And (Not IsNothing(comp.Container.Components))) Then
For i As Integer = 0 To comp.Container.Components.Count() - 1 Step 1
fixUIIn(comp.Container.Components.Item(i), style)
Next
End If
If ((Not (comp.Container Is DBNull.Value)) And (Not (comp.Container.Components Is DBNull.Value))) Then
For i As Integer = 0 To comp.Container.Components.Count() Step 1
fixUIIn(comp.Container.Components.Item(i), style)
Next
End If
我翻阅了 VB 书籍,搜索了几个论坛,所有应该起作用的东西都不起作用!很抱歉问了这样一个补救性的问题,但我只是需要知道。
正如您所知,调试器表示空对象是 comp.Container
最佳答案
更改您的 And
发送至AndAlso
标准And
将测试这两个表达式。如果comp.Container
是 Nothing
,那么第二个表达式将引发 NullReferenceException
因为您正在访问空对象的属性。
AndAlso
将使逻辑评估短路。如果comp.Container
是 Nothing
,那么第二个表达式将不会被计算。
关于vb.net - VB 中的空检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5583112/