vb.net 如何在 Visual Basic 中编码多个“或”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13393834/
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-17 11:08:29  来源:igfitidea点击:

How to code multiple "Or" in Visual Basic?

vb.nettextboxlogic

提问by Alex

I heard VB gets confused with multiple logical operators at once, so I'm stuck here. I have 3 textboxes and I want to check if any of them is empty.

我听说 VB 会同时与多个逻辑运算符混淆,所以我被困在这里。我有 3 个文本框,我想检查它们是否为空。

This simple If did not work:

这个简单的 If 不起作用:

If txt1.Text = "" Or txt2.Text = "" Or txt3.Text = "" Then -Something-

However it works if I only put two of them to compare.

但是,如果我只将其中两个进行比较,它就会起作用。

Thanks for your answers.

感谢您的回答。

回答by Carlos Landeras

The code above should work but check for null or empty string with String.IsNullOrEmpty is more elegant:

上面的代码应该可以工作,但使用 String.IsNullOrEmpty 检查 null 或空字符串更优雅:

 If String.IsNullOrEmpty(txt1.Text) Or _
   String.IsNullOrEmpty(txt2.Text) Or _
   String.IsNullOrEmpty(txt3.Text) Then
        'Do something
    End If

PD: If you use several "OR", all the conditionals will be checked.

PD:如果您使用多个“OR”,则将检查所有条件。

If you use OrElse, it will check the conditionals in order and when one it's not true the next conditional statements will not be checked

如果您使用 OrElse,它将按顺序检查条件,当条件不正确时,将不会检查下一个条件语句

回答by Asken

For or it's not confused. The above works fine.

因为或它不混淆。以上工作正常。

回答by codingbiz

Your code works. If you want the rest of the check to be ommitted you can use OrElse

您的代码有效。如果您希望省略其余的检查,您可以使用 OrElse

  If txt1.Text = "" OrElse txt2.Text = "" OrElse txt3.Text = "" Then 

  End If

or better

或更好

  If String.IsNullOrEmpty(txt1.Text) OrElse String.IsNullOrEmpty(txt2.Text) OrElse String.IsNullOrEmpty(txt3.Text) Then 

  End If