Select Case Statement in dotNet

Selct Case

When you are using IF-statement to handle multiple conditions, it is very painful to use Else or ElseIF-statement again and again. There is another statement dealing this situation rather conveniently and that is “Select-Case”-statement.
For example, you want [...]

Author : Amir Sultan

Author's Website | Articles from Amir Sultan

Selct Case
When you are using IF-statement to handle multiple conditions, it is very painful to use Else or ElseIF-statement again and again. There is another statement dealing this situation rather conveniently and that is “Select-Case”-statement.
For example, you want to make these decisions;
1. If weekday is Sunday, it is first day of the week
2. If weekday is Monday, it is second day of the week
3. If weekday is Tuesday, it is third day of the week
4. If weekday is Wednesday, it is fourth day of the week
5. If weekday is Thursday, it is fifth day of the week
6. If weekday is Friday, it is sixth day of the week
7. If weekday is Saturday, it is seventh day of the week
You can obviously do it using IF-statement
Dim DayOfWeek as String
If DayOfWeek = “Monday” Then
MsgBox(”It is second day of the week”)
ElseIf DayOfWeek = “Tuesday” Then
MsgBox(”It is third day of the week”)
ElseIf DayOfWeek = “Wednesday” Then
MsgBox(”It is fourth day of the week”)
ElseIf DayOfWeek = “Thursday” Then
MsgBox(”It is fifth day of the week”)
ElseIf DayOfWeek = “Friday” Then
MsgBox(”It is sixth day of the week”)
ElseIf DayOfWeek = “Saturday” Then
MsgBox(”It is seventh day of the week ge”)
ElseIf DayOfWeek = “Sunday” Then
MsgBox(”It is first day of the week”)
End If
You have to use seven IF-statement, let’s try to know how can we do it using select case statement.
• Create a new Window Project, say it is SelectDemo
• Set the text property of the Form to “Select Case Demo”
• Add a label and set its text property to “Input a weekday”
• Add a text box and set its id property to txtWeekday
• Add a button on the form and set its id property to btnWeekday, and set its text to “Announce”
• Double click the button and write this code
Private Sub btnWeekday_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWeekday.Click
Dim WeekDay As String = Me.txtWeekday.Text
Select Case WeekDay
Case “Monday”
MessageBox.Show(“It is second day of the week”)
Case “Tuesday”
MessageBox.Show(“It is third day of the week”)
Case “Wednesday”
MessageBox.Show(“It is fourth day of the week”)
Case “Thursday”
MessageBox.Show(“It is fifth day of the week”)
Case “Friday”
MessageBox.Show(“It is sixth day of the week”)
Case “Saturday”
MessageBox.Show(“It is seventh day of the week”)
Case “Sunday”
MessageBox.Show(“It is first day of the week”)
Case Else
MessageBox.Show(“Not a weekday.”)
End Select
End Sub
• Run the project, provide some name of the day in text box and click the button, a message box will appear saying a statement according to your input string.
How it works?
The first thing what you did in the click event of the btnWeekday is that you declare a variable and assign it the text property of txtWeekday so that it may hold a different values from textbox. Now you want to announce the result based on what value, the variable Weekday is bearing currently. Therefore you choose Weekday to use in Select case as:
Select Case Weekday
End Select
Inside the select case block you define separate case statements for each condition to be checked against. Each case-statement holding a value that may be expected and Case-Else is the option when a value is input which does not fulfill the condition. When you input the text “Sunday”, Select-Case will execute the line which is the written under the statement Case “Sunday” and a message box will appear to say, “It is first day of the week”.
Case-Insensitive Select-Case-Statement
Open the editor and simply, modify the following highlighted statement to make it case-insensitive.
Private Sub btnWeekday_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWeekday.Click
Dim WeekDay As String = Me.txtWeekday.Text
Select Case WeekDay.ToLower
Case “monday”
MessageBox.Show(“It is second day of the week”)
Case “tuesday”
MessageBox.Show(“It is third day of the week”)
Case “wednesday”
MessageBox.Show(“It is fourth day of the week”)
Case “thursday”
MessageBox.Show(“It is fifth day of the week”)
Case “friday”
MessageBox.Show(“It is sixth day of the week”)
Case “saturday”
MessageBox.Show(“It is seventh day of the week”)
Case “sunday”
MessageBox.Show(“It is first day of the week”)
Case Else
MessageBox.Show(“Not a weekday.”)
End Select
End Sub
Run the project and try writing name of a day differently.
How it works?
To convert the Select-Case Statement into case-insensitive, you have to convert variable WeekDay into lower case.
Select Case WeekDay.ToLower
It means whatever you are providing text (Whether it is ‘SUNDAY’ or ‘Sunday’ or ‘sunday’), you are converting it into lower case. Therefore you have to convert each Case-option into lower case
Case “monday”
MessageBox.Show(“It is second day of the week”)
Case “tuesday”
MessageBox.Show(“It is third day of the week”)
Case “wednesday”
MessageBox.Show(“It is fourth day of the week”)
Case “thursday”
MessageBox.Show(“It is fifth day of the week”)
Case “friday”
MessageBox.Show(“It is sixth day of the week”)
Case “saturday”
MessageBox.Show(“It is seventh day of the week”)
Case “sunday”
MessageBox.Show(“It is first day of the week”)
Multiple Selections
Open the editor and modify code again as under;
Private Sub btnWeekday_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWeekday.Click
Dim WeekDay As String = Me.txtWeekday.Text
Select Case WeekDay
Case “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”
MessageBox.Show(“It is a working day”)
Case “Saturday”, “Sunday”
MessageBox.Show(“It is a holiday”)
Case Else
MessageBox.Show(“Error! It is a false entry.”)
End Select
End Sub
Now run the project and when you will give Monday or Tuesday or Wednesday or Thursday or Friday, it will execute;
Case “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”
MessageBox.Show(“It is a working day”)
And when you will give Saturday or Sunday, it will execute;
Case “Saturday”, “Sunday”
MessageBox.Show(“It is a holiday”)
Case Else Statement
In the examples discussed above, you are observing Case else statement. For example in previous code we use
Case Else
MessageBox.Show(“Error! It is a false entry.”)
It is executed when none of the Case-statement is matched. For example, if you misspell Sunday as Snuday, then no case-statement will be executed except case-else statement. It is optional to use Case-else statement.

Like this post? Share it!

  • Tweet
  • Facebook
  • Diggit
  • Delicious
  • Diggit
  • Diggit
  • Diggit
  • Diggit
  • Diggit

Related Posts


Rss Feeds   Twitter Followers Email Updates


Community Feeds

There are no submissions added yet, be the first.

Submit More