-
March 22nd, 2006, 02:32 PM
#1
How to make VB's select/case mimic C's switch/case
in C, I can do this
number=1;
switch(number)
{
case 1:
case 2:
case 3:
printf("\nCase 1-2-3");
break;
default:
break
}
in VB when I try to do this
number = 1
select case number
case 1
case 2
case 3
case else
'none of the above
end select
the case else path is taken.
It appears that the case in C are or'd but in vb they are not
I also tried this
number = 1
select case number
case 1 or 2 or 3
case else
'none of the above
end select
but the else path was taken again.
Is there something I'm missing, or can't this be done in vb ?
I don't want to check for a range of values, just a few discrete values.
-
March 22nd, 2006, 02:40 PM
#2
Re: How to make VB's select/case mimic C's switch/case
You have to use commas:
Code:
number = 2
Select Case number
Case 1, 2, 3:
MsgBox number
Case Else
MsgBox "Not found"
End Select
... but what makes me wonder is if you actually got "Case Else" in your first example.
Did you verify that it was going to case else, either by having a breakpoint in the Case Else, a Message Box, or stepping through from the beginning of the Select? I tried what you had and it acted on Case 2, though since Case 2 had no actions before another Case (Case 3), it went to End Select immediately.
Last edited by ChaosTheEternal; March 22nd, 2006 at 02:45 PM.
-
March 22nd, 2006, 05:05 PM
#3
Re: How to make VB's select/case mimic C's switch/case
In the first instance
Code:
number = 1
select case number
case 1
'Code will execute here
case 2
case 3
case else
end select
The only way the case else would come into play is if the value of number changes before the select case.
Another thought is are you using Option Explicit? If not, perhaps you have accidentally misspelled number. Without Option Explicit, VB will automatically declare a misspelled variable as another variable.
...
-
March 22nd, 2006, 05:25 PM
#4
Re: How to make VB's select/case mimic C's switch/case
Originally Posted by ChaosTheEternal
You have to use commas:
Code:
number = 2
Select Case number
Case 1, 2, 3:
MsgBox number
Case Else
MsgBox "Not found"
End Select
>>Did you verify that it was going to case else, either by having a breakpoint
I stepped thru and watch it hit the Case 1 statement, then go around (not execute the statments under case1)
I didn't see the commas in the MSDN example.
>>Case 1 To 4, 7 To 9, 11, 13, Is > MaxNumber
My font was too small :-)
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|