VB 中的空检查
我想做的就是检查一个对象是否为空,但无论我做什么,如果它编译,它会抛出一个 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 书籍,搜索了几个论坛,但所有应该起作用的东西都不起作用!很抱歉问这样一个补救性的问题,但我只是需要知道。
如您所知,调试器表示 null 对象是 comp.Container
All I want to do is check if an object is null, but no matter what I do, if it compiles, it throws a NullReferenceException
just trying to check! Here's what I've done:
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
I've looked through VB books, searched several forums, and everything that SHOULD work doesn't! Sorry for asking such a remedial question, but I just need to know.
Just so you know, the debugger says that the null object is comp.Container
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
And
更改为AndAlso
标准
And
将测试这两个表达式。如果comp.Container
为Nothing
,则第二个表达式将引发NullReferenceException
,因为您正在访问 null 对象的属性。AndAlso
将短路逻辑评估。如果comp.Container
为Nothing
,则不会计算第二个表达式。Change your
And
s toAndAlso
sA standard
And
will test both expressions. Ifcomp.Container
isNothing
, then the second expression will raise aNullReferenceException
because you're accessing a property on a null object.AndAlso
will short-circuit the logical evaluation. Ifcomp.Container
isNothing
, then the 2nd expression will not be evaluated.您的代码比必要的更加混乱。
将
(Not (X Is Nothing))
替换为X IsNot Nothing
并省略外括号:更具可读性。 …另请注意,我已删除了多余的
Step 1
和可能多余的.Item
。但是(正如评论中所指出的),基于索引的循环无论如何都已经过时了。除非绝对必要,否则不要使用它们。使用
For Each
代替:Your code is way more cluttered than necessary.
Replace
(Not (X Is Nothing))
withX IsNot Nothing
and omit the outer parentheses:Much more readable. … Also notice that I’ve removed the redundant
Step 1
and the probably redundant.Item
.But (as pointed out in the comments), index-based loops are out of vogue anyway. Don’t use them unless you absolutely have to. Use
For Each
instead: