在 VB.NET 中选择带有 Not Condition 的情况失败

发布于 2024-10-25 04:40:39 字数 248 浏览 6 评论 0原文

如何在下面的选择案例中添加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

噩梦成真你也成魔 2024-11-01 04:40:40

我不确定我是否理解这个问题...

为什么不能像这样编写示例代码:

Select Case value
    Case 1, 2
        DoWork()
End Select

value = 0value = 3 时不会执行任何操作。提供给 Case 语句的一系列值不必是连续的。


更新回应评论:

我会这样写,利用Case Else标签:

Select Case myComboBox.SelectedIndex
    Case 1, 5, 8
        'The suggestion is acceptable, so process it
        DoWork()
    Case Else
        'The suggestion is invalid, so show an error
        MessageBox.Show("You cannot select that option. " & _
                        "Please choose options 1, 5, or 8 instead.", _
                        "Invalid Selection", _
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
End Select

当然,如果你实际上没有任何“工作”如果用户选择了正确的值,那么使用 Select Case 语句似乎就没有什么意义了。主要规则应该是使用使您的代码最清晰并且最容易理解的方式。关于 Select CaseIf 语句更快的怀疑几乎没有根据 - 编译器足够聪明,可以在几乎每种情况下产生几乎相同的结果。

I'm not sure I understand the question...

Why can't you just write the example code like so:

Select Case value
    Case 1, 2
        DoWork()
End Select

Nothing gets executed when value = 0 or value = 3. The series of values provided to a Case 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:

Select Case myComboBox.SelectedIndex
    Case 1, 5, 8
        'The suggestion is acceptable, so process it
        DoWork()
    Case Else
        'The suggestion is invalid, so show an error
        MessageBox.Show("You cannot select that option. " & _
                        "Please choose options 1, 5, or 8 instead.", _
                        "Invalid Selection", _
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
End Select

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 that Select Case is faster than an If statement—the compiler is smart enough to produce virtually equivalent results in almost every case.

北方的巷 2024-11-01 04:40:40

基于科迪所写的内容,我会选择:

Select Case value
    Case 1, 2
        DoWork()
    Case 0 ,3               
        'nothing to do, only log if needed
    Case Else
        Debug.Fail("Please provide a logical path for all possible values")
End Select

额外的分支只是为了阐明代码的意图并防止未来的更改。

Building on what Cody wrote I would go with:

Select Case value
    Case 1, 2
        DoWork()
    Case 0 ,3               
        'nothing to do, only log if needed
    Case Else
        Debug.Fail("Please provide a logical path for all possible values")
End Select

The extra branches are just to clarify the intent of the code and to guard against future changes.

长不大的小祸害 2024-11-01 04:40:40

我经常将 SELECT/CASE 与 IF/END IF 结合起来。第一个捕获范围,然后第二个对范围做类似的事情,但可能有细微的差别。

    Dim Result As Integer = 1
    Select Case Result
        Case 0 To 3
            SaveOrder()
            '0 to 3 is success 
            If Result <> 0 AndAlso Result <> 3 Then
                '1 and 2 = Everything ok
                SendCustomerMesage(0)
            Else
                '0 and 3 = Order OK, but no stock in storage
                SendCustomerMesage(1)
            End If

        Case 4 To 8
            '4 to 8 is error 
            InformCustomer(value)
        Case Else
            HandleError(value)
    End Select

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.

    Dim Result As Integer = 1
    Select Case Result
        Case 0 To 3
            SaveOrder()
            '0 to 3 is success 
            If Result <> 0 AndAlso Result <> 3 Then
                '1 and 2 = Everything ok
                SendCustomerMesage(0)
            Else
                '0 and 3 = Order OK, but no stock in storage
                SendCustomerMesage(1)
            End If

        Case 4 To 8
            '4 to 8 is error 
            InformCustomer(value)
        Case Else
            HandleError(value)
    End Select
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文