Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help - exiting Case Select statements 1

Status
Not open for further replies.

Decus

Programmer
Oct 11, 2001
20
FR
Does anyone know how to prematurely exit a Case Select statement in ASP (not ASPX). I'm trying something like:

Select Case x
Case "one"
'check second condition
If y=z Then Exit Case
'else do something
Case "Two"
'do something
Case "Three"
'do something else
End Select

Thanks in advance for your help.
 
Select Case x
Case "one"
'check second condition
If y<>z Then
'do something
END IF
'you will automatically exit!
Case &quot;Two&quot;
'do something
Case &quot;Three&quot;
'do something else
End Select
Get the Best Answers! faq333-2924
&quot;A witty saying proves nothing.&quot; - Voltaire
mikewolf@tst-us.com
 
sorry but I should have said that I'm aware that I could use If statements but also your answer would be incorrect if there were other reasons to execute the code

If y<>z Then
'do something
END IF

The &quot;do something&quot; code would only execute if y<>z - what about the other possiblities!

When x=&quot;one&quot;, I want the code to execute for ALLL posibilities except if y=z (not just if y<>z)

here's the example again...

Select Case x
Case &quot;one&quot;
'check second condition
If y=z Then Exit Case
'else do something
Case &quot;Two&quot;
'do something
Case &quot;Three&quot;
'do something else
End Select

Again, I know that I could get round this by putting in IF statements - but it seems a bit of an overkill if there is an Exit Select statement (or something of the sort) that works and would simplify the job. It's only a simple question - does a command like this exist or not?
 
There's no such thing as an
Code:
Exit Select
statement - because you don't need one!

x will never be equal to &quot;one&quot;, &quot;two&quot; AND &quot;three&quot; so it will only ever execute one of the cases and then 'exit' the statement.

As for your statement:

>> When x=&quot;one&quot;, I want the code to execute for ALLL posibilities except if y=z (not just if y<>z)

y is either going to equal z or it is not - there's no other option! Therefore using Mike's code will do EXACTLY the same as your pseudo-code.
 
Sorry, you're quite right in one aspect, it was a bad example I provided. But for more complex scenarios I believe there is a need for such a command when prematurely existing a Case Select statement is necessary/helpful.

It seems that I'm not the only one who thinks this, if you look on Microsoft's web site, under .NET development / Visual Basic, you will find the text &quot;If you do not need to execute any more of the statements in a Case or Case Else statement block, you can exit the block by using the Exit Select statement. This transfers control immediately to the statement following End Select.&quot;

This was taken from the page:
It's obvious that the advice is for vb.net / asp.net, I simply wanted to know if something similar could be done for previous versions of VB.
 
As far as I'm aware, there's no Exit Select statement in previous versions.
 
Heres a hack, put the select case function inside a function and simply call the funtion when you want to do your select case. This way when you want to exit the select you can simply do an Exit Function to escape out of it.

-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Very good suggestion Tarwn. It's a shame there's no &quot;Exit Select&quot; statements for prior .net VB code but your hack is the next best thing.

Thanks.
 
No problem, I would have posted it earlier, but RR's &quot;planned maintenance&quot; from 4am to 6am is still playing havoc with my connection at 10:30am :p

-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top