vb.net VB中的空检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5583112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:25:39  来源:igfitidea点击:

Null check in VB

vb.netnullruntime-error

提问by Supuhstar

All I want to do is check if an object is null, but no matter what I do, if it compiles, it throws a NullReferenceExceptionjust trying to check! Here's what I've done:

我想要做的就是检查一个对象是否为空,但无论我做什么,如果它编译,它就会抛出一个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

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.

我浏览了 VB 书籍,搜索了几个论坛,但所有应该工作的东西都没有!很抱歉提出这样一个补救问题,但我只需要知道。

Just so you know, the debugger says that the null object is comp.Container

只是你知道,调试器说空对象是 comp.Container

回答by Ken Pespisa

Change your Ands to AndAlsos

将您的Ands更改为AndAlsos

A standard Andwill test both expressions. If comp.Container is Nothing, then the second expression will raise a NullReferenceException because you're accessing a property on a null object.

一个标准And将测试这两个表达式。如果 comp.Container 为 Nothing,则第二个表达式将引发 NullReferenceException,因为您正在访问 null 对象上的属性。

AndAlsowill short-circuit the logical evaluation. If comp.Container is Nothing, then the 2nd expression will not be evaluated.

AndAlso将短路逻辑评估。如果 comp.Container 为 Nothing,则不会计算第二个表达式。

回答by Konrad Rudolph

Your code is way more cluttered than necessary.

你的代码比必要的更混乱。

Replace (Not (X Is Nothing))with X IsNot Nothingand omit the outer parentheses:

更换(Not (X Is Nothing))X IsNot Nothing和省略外括号:

If comp.Container IsNot Nothing AndAlso comp.Container.Components IsNot Nothing Then
    For i As Integer = 0 To comp.Container.Components.Count() - 1
        fixUIIn(comp.Container.Components(i), style)
    Next
End If

Much more readable. …?Also notice that I've removed the redundant Step 1and the probably redundant .Item.

更具可读性。...?还要注意,我已经删除了多余的Step 1和可能多余的.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 Eachinstead:

但是(正如评论中指出的那样),无论如何,基于索引的循环已经过时了。除非绝对必要,否则不要使用它们。使用For Each来代替:

If comp.Container IsNot Nothing AndAlso comp.Container.Components IsNot Nothing Then
    For Each component In comp.Container.Components
        fixUIIn(component, style)
    Next
End If