By: Issac in VB.net Tutorials on 2008-11-25
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
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
State = "Diet Ruined"
Case "Not Eaten"
State = "Diet Not Ruined"
State = "Didn't check"
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:
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 "Not Eaten"
Note also that it will only look for an exact match - "Eaten", but not "eaten".
The next line to examine is this:
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
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"
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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in VB.net )
Latest Articles (in VB.net)
- Data Science
- React Native
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews