vb.net If 多个值的条件

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

If Condition for multiple values

vb.netif-statement

提问by Chow.Net

I have a dropdown list where i have list of values from 0 TO 9

我有一个下拉列表,其中包含 0 到 9 的值列表

If the selected value is 1,2,3,4 or 5 then EmployeeName field cannot be blank.

如果所选值为 1、2、3、4 或 5,则 EmployeeName 字段不能为空。

Please help me to write this condition in vb.net.

请帮我在 vb.net 中写这个条件。

Thanks in advance

提前致谢

回答by Matt Wilko

I think your best bet is to use a Selectstatement. This makes it easy to maintain your code if you change what each value does:

我认为最好的办法是使用Select声明。如果您更改每个值的作用,这可以轻松维护您的代码:

Select Case CInt(ComboBox.Value)

Case 1 To 5
    'Employee field cannot be blank
Case Else
    'Employee field can be blank
End Select

回答by Grant Thomas

You can string multiple conditions together in one, such that:

您可以将多个条件串成一个,例如:

If (thing = 1 OrElse thing = 2 OrElse thing = 3 OrElse thing = 4 OrElse thing = 5) 

End IF

However, you could add these values to a whitelist, and do an Anyor Containscheck. You could of course to a lower thanand greater thancomparison to constrain, but I didn't risk that without enough detail.

但是,您可以将这些值添加到白名单中,然后执行AnyContains检查。您当然可以使用小于大于比较来进行约束,但如果没有足够的细节,我不会冒险这样做。

回答by marc casas

if SelectedValue <= 5 And SelectedValue >= 1 then
' EmployeeName cannot be blank