Hi,
Is there a rule-of-thumb as to when to use Select Case or If/Then/Else statements?
What are the pros and cons to each?
Thanks,
Al.
Printable View
Hi,
Is there a rule-of-thumb as to when to use Select Case or If/Then/Else statements?
What are the pros and cons to each?
Thanks,
Al.
There is no Rule of Thumb but it is considered good parctice to use Select Case when you have a number of conditions. Using If, if you have several conditions createds code that is not easily read...
If this then
do that
if that then
do this
if nothing then
do something
endif
endif
endif
Add a few else if's and you have a hornets nest.
Case on the other hand is very easily read.
Case 1
do this
Case 2
do that
Case 3
Got it
If anyone know if there's gonna be any expanding and collapsing of if/for/do/while/select in vb7 i would be glad to know...
Usually If...Then is an Either THIS or THIS statement. In other words, it's usually only for 2 outcomes.
Meg, no you can have elseif's between, so actually there isn't much differing it from select case (i havent tested performance yet)
Select Case can also reduce the amount of code when you have a number of conditions, e.g. in an error handler
versusCode:Select Case Err.Number
Case 1
Msgbox "Yadda Yadda 1"
Case 2
Msgbox "Yadda Yadda 2"
Case 3
Msgbox "Yadda Yadda 3"
Case 4
Msgbox "Yadda Yadda 4"
Case 5
Msgbox "Yadda Yadda 5"
Case 6
Msgbox "Yadda Yadda 6"
Case Else
Msgbox "Yadda Yadda Else"
End Select
Code:If Err.Number = 1 Then
MsgBox "Yadda Yadda 1"
Elseif Err.Number = 2 Then
MsgBox "Yadda Yadda 2"
Elseif Err.Number = 3 Then
MsgBox "Yadda Yadda 3"
Elseif Err.Number = 4 Then
MsgBox "Yadda Yadda 4"
Elseif Err.Number = 5 Then
MsgBox "Yadda Yadda 5"
Elseif Err.Number = 6 Then
MsgBox "Yadda Yadda 6"
Else
MsgBox "Yadda Yadda Else"
End if
not the final truth, but a hint in the (hopefuly right direction). as with all rules, there are plenty exeptions. I wouldn't bet, but i thing 'Select Case' is faster.
if you have to check several conditions of one variable, 'Select Case' is probably more elegant:
Select Case i
Case 1, 6, 9, 27: do something
Case 2, 11, 14: do something different
Case Is > 30: that's a lot
Case Else: everything else
End Select
if you need to check condiditions for more variables, 'If Then Else' could produce shorter code:
If i >= 1 And j < 255 Then
ElseIf Whatsoever
End If
I just tested both and the ifs are definitely faster...
Yes, same results here
Not sure about VB but in C the case statement is faster but in C there is no "elseif" statement, just an else followed by another if. so all the expressions get evaluated unlike the case statement.
VB has an elseif statement so ....?
dunno !!
I use case statements if I have more than two elseifs or if I think more will be added later. it's easier to read.
Ok you have an object which can have two colours...red or blue, therefore an IF statement
If Object.color = Blue Then
'whatever
else
' colour is not blue therefore red
End If
Now if the object could be red, blue, yellow, green etc
Select Case Object.colour
case red
'whatever
case blue
'whatever
case yellow
'whatever
case green
'whatever
case Else ' could be Otherwise caren't remember
'whatever
End Select
Use If...Then...Else for either/or conditions, Case for multiple conditions.
The fact that you can use Else If doesn't equate an If statement to a Case statement kedaman:0
A fellow Aussie to the rescue eh? :D
How was the free train ride the other day?
Didn't bother with it...another government stunt to appease the populous....work in North Ryde...no station anyway.
Q. Whats the difference between a Ferret and John Howard?
A. A ferrit can go down your pants, John Howard is only interested in your back pocket
He's a bit of looser.
I read the SMH every day from Tokyo on the net to keep up to date.
He got screwed on the Harbour Bridge walk :D
How many John Howards does it take to change a light bulb?
One to promise it
one to argue whether the service needs GST
One to debate it
But it won't happen
a) How many posts that it take to change a lightbulb?
b) How many lightbulbs does it take for a programmer to see anything?
c) How many lightbulbs do you get with 20 finnish mark?
d) How long does a normallifetime 40W 50hz last?
e) How dark is it in Tokyo at now?
f) How many lightbulbs do you need?
a) 1
b) 0 (Computer screen gives enough light)
c) 40
d) 750hrs
e) Very, except for the neon
f) 1 flourescent tube would be fine... email it to me plwase
I don't know the Yens rate to mk but its probably between 1:20 to 1:30
but 1$ is about 5mk or 6€
I was going to look it up since I was online but I thought it'd be easier to take a guess, wrongly it seems.
The yen is 'suffering' from EnDaka, meaning it's very expensive, 100y = US$1.07
The good thing is, AUD$1.00 = 61Y because the Aussie PM is a moron so I get a fat bonus here which is worth say the price of 10 fridges, but when I send the money to Australia (Oz) it's worth about 25 fridges. And, considering the difference in cost of living between Tokyo and Sydney I can live on one bi-annual bonus for 4-5 months in Sydney!
If I stay here for another couple of years before I head home and keep playing with my E*trade account I might be able to buy a house!!!!! (in sydney of course, not tokyo - where I struggle to afford a post office box )
The Aussie PM is a mental giant compared to Alexander Downer, Andy Pandy, the teletubbies, and Pauline Hanson........actually Hanson and Howard have about the same IQ, loitering around the low 40s:)
She got screwed again in NSW, de-registered!!!!!
Serves the racist ***** right.
Did she do the reconciliation walk over the bridge? not bloody likely.
Poiliticians suck in every country though, you should see this place, when it comes to corruption, maybe the Aussie poli's being stupid is not so bad. Public money just vanishes here