+ Reply to Thread
Results 1 to 2 of 2

Thread: VB.NET from beginner to advanced programmer Part 15 - Select Case Statements

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    VB.NET from beginner to advanced programmer Part 15 - Select Case Statements

    Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.


    VB.NET from beginner to advanced programmer
    1. Introduction and Installation
    2. Objects and Events
    3. Variables
    4. The basic data types
    5. Logical Operators
    6. Relational Operators
    7. If statements Then
    8. Arithmetical Operators
    9. Loops Part 1
    10. Arrays
    11. Loops Part 2
    12. Try Catch statements
    13. Subs and Functions
    14. Difference between Scopes
    15. Select Statements
    16. Multidimensional arrays
    17. Structures
    18. Classes
    19. Enumerations
    20. Advanced Comments
    21. Compiling Directives





    Remember I talked about an option to if statemetns earlier? The option is called Select Case statements. The select case statement is good to use when you want different blocks of code to be executed depending on different values on one variable. If statements is good if you only need to test one value or if you have to test different variables(can't be done with select case).

    Anyway, to use a select case statement you have to create it by writing "Select Case" and then the value you want to test. End the statement with "End Select". Could look something like this:

    Code:
            Select Case "test"
    
    End Select
    Code:
            Select Case myVariable
    
    End Select

    At the moment our select case statements are empty, inside them we will add cases by writing "Case " and then the value for that case. Then the program will get the value of the select case statement(as we did above) and then check if that value is the same as the first case. If it is the associated block of code will be run or else it will try next until it founds one. You can also add an "Case Else" block at the end which will be executed if no "normal" case's condition has been true. A few examples:


    Code:
     
    Select Case myVariable
            Case 1
                    MessageBox.Show("myVariable = 1")
             Case 2
                    MessageBox.Show("myVariable = 2")
            Case 3, 4
                    MessageBox.Show("myVariable = 3 or 4")
            Case 5
                    MessageBox.Show("myVariable = 5")
            Case Else
                    MessageBox.Show("myVariable is nether 1,2,3,4 or 5")
    End Select
    Code:
     
    Select Case "CodeCall"
            Case "test"
                MessageBox.Show("Testing...")
            Case "CodeCall"
                MessageBox.Show("CodeCall rules!")
            Case "hi"
                MessageBox.Show("Hello")
    End Select


    Observe that the select case statement will always only run a maximum of one block even if more then one's condition is True. In the example below we will only see one messagebox with the message "This will be shown".


    Code:
    Select Case 5
            Case 321
                MessageBox.Show("This is False")
            Case 5
                MessageBox.Show("This is True and will be shown")
            Case 5
                MessageBox.Show("This is True but won't be shown")
    End Select




    More advanced Select Case Statements


    To:


    When using some sort of values(Integers for example) you can add a whole range of numbers to one case by using "[number] To [number]", like this:


    Code:
    Select Case myVariable
            Case 0 To 5
                MessageBox.Show("myVariable is between 0 and 5")
            Case 6 To 10
                MessageBox.Show("myVariable is between 6 and 10")
            Case 11
                MessageBox.Show("myVariable = 11")
    End Select

    Is:

    You can also add relational operators to your select case statement. Do this by replace "Case [value]" with "Case Is [relational operator] [value]". Actually a select case statement does always contains a relational operator, the is equals to operator (=) even though it's hidden. This means that "Case Is = myValue" is the same as "Case myValue". Here comes a little example on how to use it:



    Code:
    Select Case myVariable
            Case Is > 0
                MessageBox.Show("myVariable is greater then 0")
            Case Is < 0
                MessageBox.Show("myVariable is less then 0")
            Case Else
                MessageBox.Show("myVariable is equals to 0")
    End Select




    And that's what I had about Select Case Statements. If you don't know when to use them and when to use If Statements you can look here for some tips: If Statements VS Switch Statement - CodeCall Programming Wiki Note that that article is written generally for all Programming languages and therefor the Select Case statement is called Switch Statement. Hope I didn't lost you there. See you in the next part.
    Last edited by Vswe; 03-21-2010 at 03:35 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: VB.NET from beginner to advanced programmer Part 15 - Select Case Statements

    Select statements are so much easier to read than If statements, so much cleaner IMO. I've read that they are faster as well but never benchmarked them. Nice work +rep!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 11-02-2009, 05:46 PM
  2. Replies: 3
    Last Post: 11-02-2009, 05:37 PM
  3. Replies: 1
    Last Post: 11-02-2009, 06:05 AM
  4. Replies: 1
    Last Post: 11-02-2009, 05:46 AM
  5. Replies: 1
    Last Post: 11-02-2009, 05:43 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts