4

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
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
kurozakura
  • 2,339
  • 8
  • 28
  • 41

3 Answers3

12

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Lets say 1,2,3,4...n aren't just values and they have meaning lets say they are the IDs from a drop-down/combo-box list and user selects a value. Lets say user is only allowed to select when its 1,5,8 and else show a message. How do u put that in a single case statement? – kurozakura Mar 24 '11 at 02:15
  • @user: I updated my answer with another code example. Does that help any? – Cody Gray - on strike Mar 24 '11 at 11:02
  • I have thought about this as well, but just wanted to know if it can be done in single case statement . like If Not (1,5,8) then showMessagebox() .Instead of If (1,5,8) DoWork() Else ShowMessagebox(). – kurozakura Mar 25 '11 at 02:37
  • @user: No. You can't do that. And there's absolutely no reason to do so, either. VB.NET is the only language that supports the `Case 1, 2, 3` syntax. You can't even write that in C#. You need `If (i = 1) OrElse (i = 5) OrElse (i = 8) Then`. What do you think is **wrong** with that, exactly? – Cody Gray - on strike Mar 25 '11 at 03:36
  • just wanted to know if it was possible that's all. – kurozakura Mar 28 '11 at 03:42
  • @CodyGray you probably know it, but that is just plain wrong. C# Does support something like the `Case 1,2,3` , because there is a fallthrough and you have to break the switch manually. – Mafii Dec 01 '16 at 09:03
  • @Mafii Well, that's true of C and C++, but C# doesn't have fall-through for case statements. You can certainly simulate it, but you'd have to use a goto, and [who knows what kinds of bad things that might cause](https://xkcd.com/292/)! – Cody Gray - on strike Dec 02 '16 at 22:40
  • @CodyGray Not sure wheter I misunderstood something - here's an example: http://tryroslyn.azurewebsites.net/#K4Zwlgdg5gBAygTxAFwKYFsDcAoUlaIoYB0AMpAI457TxJrrEDCA9gDZuoDGyYLEIYgHFUEVACcwXauFoBZKeJYgWAM2TM4ACwCG4gA45s+4ACM2UmFzY6QIGExgBvbDDcxX7k+ctMAFACUnm4u7mEwIADuYMhcWn4iyABqOmzAqIEBoeE5XLaoMAAMAFzBOe55IAUAjKXl4ZUFAEx19e4AkKbiqDoA1jht7gAmqKo6wGzIrYPtyFpKkTBiiwByLMgAkuj6nOiiaEMAogAeXKj6vPyBA4Me9QC+ZY9hZd4WXDCQyDDHzlCoyEw9xgAF4YABWG5uMqmFjsGBoFBQu5eSQANx0aE+EG+iRSaQyQTC2XCYFUMD8iOQRJyJPqVNBCPE6WR5W6yGA4ggMAAzKy3M9ymUwuzOdzqsjHvcgA=== isn't that a switch fall-through? – Mafii Dec 03 '16 at 23:10
  • @Mafii Um, I opened that, but I can't see any code anywhere. All of the boxes are blank. Refreshing or using another browser doesn't seem to help, so I'm not sure what I'm doing wrong. Anyway, I figure what you are probably talking about is that it does allow a single block to handle multiple cases, so `case 1: case 2: { … }` is perfectly fine. However, no block is actually allowed to fall through, so `case 1: { …}` cannot fall through to a subsequent `case 2: { … }`. They made this decision to avoid a common programming pitfall in C, at the expense of making Duff's Device impossible to code! – Cody Gray - on strike Dec 04 '16 at 12:27
  • @CodyGray weird, well yes, this is what I'm talking about. Thanks for the heads up! – Mafii Dec 05 '16 at 08:44
1

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.

Ando
  • 11,199
  • 2
  • 30
  • 46
0

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
Stefan
  • 11,423
  • 8
  • 50
  • 75
  • A reasonable example, certainly, so pardon my nitpicking. But the real question that comes to mind is why you use return values to indicate errors. Unless this is really low-level code, exceptions are probably a better option. And even if you *do* return error codes, definitely use an enum for readability's sake. – Cody Gray - on strike Mar 24 '11 at 11:12
  • @Cody it was only sample code I wrote up for this answer, not production code. ;) – Stefan Mar 24 '11 at 12:10