6

So I have something like the following in Vb6;

Select case Case

case "Case0"
...

case "Case1"
  if Condition Then
     Exit Select
  End If
  *Perform action*

case "Case2"
...

End Select

But for some reason my Exit Select throws the error Expected: Do or For or Sub or Function or Property. I know, not pretty. Should I be using something else? I could just use if statements and not exit the case early, but this would require duplicate code, which I want to avoid. Any help would be really appreciated.

Update

Tried changing Exit Select to End Select and got the error End Select without Select Case. It is definitely within a Select Case and an End Select.

Deanna
  • 23,876
  • 7
  • 71
  • 156
windowsgm
  • 1,566
  • 4
  • 23
  • 55
  • Is that your actual code? You have defined `Case1` twice, if so. – Widor May 23 '12 at 15:00
  • What are the data types of your enumerated variable and the values you're comparing it to? – Widor May 23 '12 at 15:18
  • @Widor The variable is a DB call of a String and it's being compared to Strings also. – windowsgm May 23 '12 at 15:21
  • Can you update your code to be valid VB, because it would throw all sorts of errors at present and you need to rule them out first. E.g.`Case Case1` should be `Case "Case1"` etc. – Widor May 23 '12 at 15:26
  • Waaaaaay too much code for me to just copy across and it was working fine before I began to edit this case statement so we can rule out problems with the rest of the code. Plus the compiler is specifically pointing to the `Exit Select`. – windowsgm May 23 '12 at 15:30
  • Sorry - just noticed this is VB6 - see answer! – Widor May 23 '12 at 15:38

7 Answers7

8

VB doesn't have a facility to exit a Select block. Instead, you'll need to make the contents conditional, possibly inverting your Exit Select conditional.

Select case Case 

case "Case0" 
... 

case "Case1" 
  If Not Condition Then 
    *Perform action* 
  End If 

case "Case2" 
... 

End Select 

Which will have exactly the same end result.

Deanna
  • 23,876
  • 7
  • 71
  • 156
  • +1. And make "perform action" into a subroutine for readability – MarkJ May 23 '12 at 16:14
  • 1
    The logic was quite complicated in the real version and it would have just been handy to have had a `End Select`, but I ended up going with something along these lines anyways. – windowsgm May 24 '12 at 07:33
  • Doesn't answer the question as stated. Sorry but in some cases, you really do want to exit early. – Henrik Erlandsson Mar 09 '18 at 13:30
  • @HenrikErlandsson I've just added an opening paragraph explaining a bit more. Hopefully that's better? – Deanna Mar 13 '18 at 08:52
5

There is no Exit Select Statement in VB6 - only VB.NET

Have a look at the language reference for the Exit Statement - there is no mention of Exit Select

Best option is to refactor your select statements into a new subroutine and then just Exit Sub

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
3

Unfortunately, VB6 doesn't have the Exit Select clause available.

This is ony available in VB.NET!

Widor
  • 13,003
  • 7
  • 42
  • 64
2

Try this

Do
    Select case Case

    case "Case0"
    ...

    case "Case1"
      if Condition Then
         Exit Do
      End If
      *Perform action*

    case "Case2"
    ...

    End Select
Loop While False

Edit: Btw, I wouldn't hesitate to use GoTo in this case (and not only this).

wqw
  • 11,771
  • 1
  • 33
  • 41
  • Agreed on `GoTo`. I'm not sure I could endorse the `Do ... Loop While False` though... :p – Deanna Mar 13 '18 at 08:55
  • @Deanna `do { ... } while(0)` is very popular plain C idiom for early exit (and correct macro expansion). I really do hope VB6 equivalent `Do/Loop` is a noop for the compiler as well. – wqw Mar 14 '18 at 08:23
1

Just to answer this old question the other way you can also use GoTo:

Select case Case

case "Case0"
...

case "Case1" 'you can use not condition as stated

  if Condition Then GoTo Exit_select
  *Perform action*

case "Case2"
...
Exit_select:
End Select
0

just discovered some thing very trivial does the trick:

Select case Case 

case "Case0" 
... 

case "Case1" 
  A=A

case "Case2" 
... 

End Selec
Damien
  • 1,219
  • 13
  • 23
Robert
  • 1
0

there is always GOTO to jump to a label at the end.