The Select Case statement in VB.net

By: Issac Emailed: 1697 times Printed: 2203 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

For example, we have that the consequences of eating the ice-cream are that the Diet will be either "Ruined" or "Not Ruined". But what if we add another option - "Diet Not Tested". In other words, we ate the ice cream but refused to climb onto the scales to weigh ourselves!

With three choices, we can use If ... Else statement. But let's employ Select Case statement. Remember: all we are doing is testing what is inside a variable, in this case a variable called icecream. Once we decide what is inside the variable, we can take some action. So let's see how the Select Case works.

Create a Form with a button and a Textbox on it. Double click the new button. You will see something like this

Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _Handles Button1.Click

End Sub

Between the button Sub and End Sub code add the following

Dim icecream As String

Dim State As String

icecream = TextBox1.Text

Select Case icecream

Case "Eaten"

State = "Diet Ruined"

Case "Not Eaten"

State = "Diet Not Ruined"

Case Else

State = "Didn't check"

End Select

MsgBox State

Run your code to test it out. Click inside your textbox and enter the word "Eaten". Then click your button to see what happens. Now enter the words "Not Eaten" and click your button. Next, try the word "eaten", with a lowercase "e".

In the code above, we first set up two variables called icecream and State. Next, we transfer whatever is in Textbox1 to the variable icecream. The Select Case begins on the next line:

Select Case icecream

We tell Visual Basic that we want to start a Select Case statement by simply using the words "Select Case". This is enough to set up the statement. The variable icecream follows the words Select Case. We're saying, "Set up a Select Case statement to test what is inside the variable called icecream ". The next line is this:

Case "Eaten"

We ask Visual Basic to check if the variable icecream contains the word "Eaten".

If it is the Case that icecream contains the word "Eaten", then VB will drop down to the line or lines of code below and read that. If the variable icecream doesn't contain the word "Eaten", the programme will skip the line or lines of code below and jump to the next Case.

The programme will continue to check all the words after Case to see if one of them contains what is in the variable icecream. If it finds one, it will read the code below the Case word; if it doesn't find any matches, it will not do anything at all. In our code, we're checking these three Cases:

Case "Eaten"

Case "Not Eaten"

Case Else

Note also that it will only look for an exact match - "Eaten", but not "eaten".

The next line to examine is this:

Case Else

You can use the Else word after Case. If the program hasn't found any matches, it will then execute the code below the Case Else line.

The final line to examine is

End Select

All we're doing here is to tell Visual basic to end the Select Case statement.

So the Select Case checks a variable for any number of different choices. If a match is found, it will then execute code below the Case option it has found. In our code, we've just told the program to store some text in the State variable. After the Select Case statement has ended we displayed the variable in a Message Box.

You can use Select Case statement with numbers, as well, and the To word comes in very handy here. If you were checking a variable to see if the number that was in the variable fell within a certain age-range, you could use something like this:

Select Case range

Case 16 To 21

MsgBox “Still Young”

Case 50 To 64

MsgBox “have hair-dye or wig”

End Select

Here, a variable called range is being tested. It's being checked to see if it falls between certain values. If it does, then a message box is displayed.


VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

I Want more Advanced Case Shown...
I Know Everything about C# and are Currently checking out VB.
Please Post a Advanced Case Statement ;)


View Tutorial          By: Electron at 2009-11-23 02:32:07

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Issac
Save/Write/Read image file from/to a database using Java program
How to modify the objects using java classes
Java program for Cloning
Java program for changeable wrapper class
A tutorial on Chat Server and Chat Client in VB.net
Chat Server in VB.net
Chat client in VB.net
How to open and read an XML file in VB.net
How to create an XML file in VB.net
XML and VB.net
How to create an XML file in VB.NET using Dataset
Multi Threaded Client Socket Programming in VB.net
Multi Threaded Server Socket Programming in VB.net
Multi threaded Socket Programming in VB.net
Client Socket Program sample in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net

More Latest News
Most Viewed Articles (in VB.net )
Your first VB.NET Crystal Reports - A step by step guide
Nested If and Single line if statement
What is .NET Framework and the Common Language Runtime?
Sub Procedures and Functions in VB.net
Visual Basic Statements
Arrays and Dynamic Arrays in VB.net
Vb.net binary reader and writer
To read the entire worksheet in an Excel workbook through VB.net Code
Update cell data in an Excel file using OLEDB in VB.net
Chat Server in VB.net
Creating Tree Views in Code using VB.net
The Select Case statement in VB.net
Data types in VB.net
Operators in VB.net
If…Else Statements in VB.net
Most Emailed Articles (in VB.net)
How to export from database to excel using VB.net
Changes in Controls from VB6 to VB.net
What is .NET Framework and the Common Language Runtime?
While Loop in VB.net
Substring in Vb.Net String Class
Create an Excel 2007 file in VB.NET 2005
Insert Pictures in Excel 2007 from VB.NET 2005
Socket Programming in VB.net
Chat Server in VB.net
Using the ServiceBase Class in VB.net
Using Select Case in VB.net
String Insert, index off & format in VB.net
String Split function in VB.net
Vb.net text reader
Format Excel 2007 Page in VB.NET