Results 1 to 5 of 5

Thread: [RESOLVED] Cases and If Statements

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Location
    Altoona, PA
    Posts
    242

    Resolved [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

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Location
    Altoona, PA
    Posts
    242

    Re: Cases and If Statements

    Quote Originally Posted by Hack View Post
    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:
    1. if '....then
    2. '...
    3. elseif '....then
    4. '...
    5. elseif '....then
    6. '...
    7. else '....then
    8. '...
    9. end if
    ...does look sloppy

    Thanks for the help

  5. #5
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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
  •  



Click Here to Expand Forum to Full Width