|
-
Jun 15th, 2009, 12:56 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Cases and If Statements
Okay, so I've been wondering what exactly the difference between using cases and if statements are. It seems to me that they do exactly the same thing, but I'm sure that there are situations where one is better to use than the other -- What would these situations be?
I was wondering if someone could give me an example to show the difference.
Please and Thank you
-
Jun 15th, 2009, 01:08 PM
#2
Re: Cases and If Statements
They are more or less the same thing, but a Select Case is more compact, see http://www.homeandlearn.co.uk/NET/nets1p21.html
-
Jun 15th, 2009, 01:31 PM
#3
Re: Cases and If Statements
If you have lots of possibilities that need to be evaluated, Select Case is much easier to read.
The difference between the two is something for the programmer. The end user could care less.
-
Jun 15th, 2009, 01:34 PM
#4
Thread Starter
Addicted Member
Re: Cases and If Statements
 Originally Posted by Hack
If you have lots of possibilities that need to be evaluated, Select Case is much easier to read.
The difference between the two is something for the programmer. The end user could care less.
Yeah that's one thing I noticed. I wasn't sure if the two acted the same way and it seems like they do.
VB.NET Code:
if '....then
'...
elseif '....then
'...
elseif '....then
'...
else '....then
'...
end if
...does look sloppy 
Thanks for the help
-
Jun 15th, 2009, 01:59 PM
#5
Re: [RESOLVED] Cases and If Statements
Select case is most useful when checking a single value, Whereas the If/Elsif is more appropriate for checking multiple conditions:
If IsMonday then
' Do the Laundry
ElseIf FortyHoursOfWorkDone Then
' Play Golf
ElseIf ComputerBroken Then
' Play Golf!
Else
' Do Work 
EndIf
However, even the above can be converted to a Select Case statement, even if it may be a bit hard to read depending on your conditions:
Select Case True
Case IsMonday
Case FortyHoursOfWorkDone
Case ComputerBroken
etc.
Note that VB implicitly exits the case statement when the first match is found. Unfortunately, reading it does not necessarily imply that this is so (particularly if the reader is from a C background). The If/Then may be a better solution, but as already stated, it's programmers choice.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
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
|