在 VB.NET 中选择带有 Not Condition 的情况失败
如何在下面的选择案例中添加 Not 条件。
是<>适用于单个值,“To”适用于范围,但该值是特定值,而不是一系列数字。在这种情况下是否可以使用 select case,或者我是否必须切换到 if-else。例如:我不希望 case 在值为 0 和 3 时执行
Select value
case 0,1,2,3
End Select
How to Add Not condition in the below select case.
Is <> works for single value, and 'To' works for a Range but the value are specific values there are not series of numbers. Is it possible to use select case in this scenario, or do i have to switch to if-else. Ex: i don't want case to execute when value is 0 and 3
Select value
case 0,1,2,3
End Select
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定我是否理解这个问题...
为什么不能像这样编写示例代码:
当
value = 0
或value = 3
时不会执行任何操作。提供给Case
语句的一系列值不必是连续的。更新回应评论:
我会这样写,利用
Case Else
标签:当然,如果你实际上没有任何“工作”如果用户选择了正确的值,那么使用 Select Case 语句似乎就没有什么意义了。主要规则应该是使用使您的代码最清晰并且最容易理解的方式。关于
Select Case
比If
语句更快的怀疑几乎没有根据 - 编译器足够聪明,可以在几乎每种情况下产生几乎相同的结果。I'm not sure I understand the question...
Why can't you just write the example code like so:
Nothing gets executed when
value = 0
orvalue = 3
. The series of values provided to aCase
statement doesn't have to be sequential.Update in response to comment:
I would write that like this, taking advantage of the
Case Else
label:Of course, if you don't actually have any "work" to be done in the case that the user selects the correct value, there seems little point in using a
Select Case
statement at all. The cardinal rule should be to use whichever makes your code the clearest and easiest to understand. There's little validity to the suspicion thatSelect Case
is faster than anIf
statement—the compiler is smart enough to produce virtually equivalent results in almost every case.基于科迪所写的内容,我会选择:
额外的分支只是为了阐明代码的意图并防止未来的更改。
Building on what Cody wrote I would go with:
The extra branches are just to clarify the intent of the code and to guard against future changes.
我经常将 SELECT/CASE 与 IF/END IF 结合起来。第一个捕获范围,然后第二个对范围做类似的事情,但可能有细微的差别。
I often combine SELECT/CASE with IF/END IF. The first one to catch a range, then second one to do similar things with the range, but maybe with small differences.