Tuesday, April 29, 2014

Visual Basic Tutorial - How to use Select Case in Visual Basic 2008-2010 for Beginners

Hey guys today we are going to use another usable condition can be used to run multiple expressions, this statements is the 'Select Case Statement'..

Select Case - Runs one of several groups of statements, depending on the value of an expression. This statement can handle 1 to 10 or 10 to thousand statements or depending to the expression want by the programmers.

The Basic Structure of Select Case Statement, this statement require you to declare a variable to be used. See Parts

Select [ Case ] testexpression 

 [ Case expressionlist [ statements ] ] 
 [ Case Else [ elsestatements ] ]
End Select

Parts of Select Case Statement from support.microsoft.com

Testexpression - Required. Expression. Must evaluate to one of the elementary data types (Boolean, Byte, Char,Date, Double, Decimal, Integer, Long, Object, SByte, Short, Single, String, UInteger, ULong, and UShort).

Expressionlist - Required in a Case statement. List of expression clauses representing match values fortestexpression. Multiple expression clauses are separated by commas. Each clause can take one of the following forms: 

• expression1 To expression2 
• [ Is ] comparisonoperator expression 
• expression

Use the To keyword to specify the boundaries of a range of match values for testexpression. The value of expression1 must be less than or equal to the value of expression2.


Use the Is keyword with a comparison operator (=, <>, <, <=, >, or >=) to specify a restriction on the match values for testexpression. If the Is keyword is not supplied, it is automatically inserted before comparisonoperator.


The form specifying only expression is treated as a special case of the Is form wherecomparisonoperator is the equal sign (=). This form is evaluated as testexpression = expression.

The expressions in expressionlist can be of any data type, provided they are implicitly convertible to the type of testexpression and the appropriate comparisonoperator is valid for the two types it is being used with.

statements - Optional. One or more statements following Case that run if testexpression matches any clause in expressionlist.

elsestatements - One or more statements following Case Else that run if testexpression does not match any clause in the expressionlist of any of the Case statements.

End Select - Terminates the definition of the Select...Case construction. 

Now that you already know the basic parts of the Select Case Statement, we will going to create a sample project that will use Select Case Statement.

Create a New Project and Name it [Age]

This program will determine whether the inputted age is Infant, Teen or Adult.

Controls you need

1 textbox = type desired age when program is executed
1 label = rename it to "type age"
1 button = rename it to "identify age"

The Code: Make sure you embed this code to the 'identify age' button_click event.

Dim age as integer = Textbox1.Text
    Select Case age
       Case 0 To 10
            Msgbox("This is Infant")
       Case 15 To 19
            Msgbox("This is Teen")
       Case 20 To 90
            Msgbox("This is Adult")
    End Select
After you write this code to your project try to run it and try if it works.. 

I have a made a video tutorial on this program on how you can do this project. 

Watch Video Tutorial



No comments:

Post a Comment