Greg Owen
Over 27 Years of Programming Experience
Programming Examples, Portfolio, and More
Home

Select Case Statement and Switch Statement

Visual Basic and C# Programming

Select Case Statement and Switch Statement

Select Case Statements and Switch Statements can be used to replace If Then statements in Visual Basic and Visual C#. If you are using more than two If-Then statements, it is a good idea to use a Select Case or a Switch statement.

Steps for testing this code:

Create a new Windows Application Visual Studio Project using either Visual Basic or Visual C#.

A new form will automatically be created.
Add the following controls to the form:

ComboBox
Textbox
Two buttons
Name the ComboBox, ‘cmbValue’.
Name the TextBox, ‘txtOutput’.
Name one of the buttons, ‘pbSwitch’.
Name one of the buttons, ‘pbIf’.
Add the following values to the List in the ComboBox:

1
2
3
4
5
6

Copy and Paste the code below into your Visual Basic Project:

      Private Sub pbSwitch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbSwitch.Click
      ProcessSwitch()
      End Sub
      Private Sub ProcessSwitch()
      Dim sOutput As String = ""
      Dim nVal As Integer = Val(cmbValue.Text)
      Select Case nVal
      Case 1
      sOutput = "A"
      Case 2
      sOutput = "B"
      Case 3
      sOutput = "C"
      Case 4
      sOutput = "D"
      Case 5
      sOutput = "E"
      Case Else
      sOutput = "Invalid Data"
      End Select
      txtOutput.Text = sOutput
      End Sub

      Private Sub pbIf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbIf.Click
      ProcessIf()
      End Sub
      Private Sub ProcessIf()
      Dim sOutput As String = ""
      Dim nVal As Integer = Val(cmbValue.Text)
      If nVal = 1 Then
      sOutput = "A"
      End If
      If nVal = 2 Then
      sOutput = "B"
      End If
      If nVal = 3 Then
      sOutput = "C"
      End If
      If nVal = 4 Then
      sOutput = "D"
      End If
      If nVal = 5 Then
      sOutput = "E"
      End If
      If nVal > 5 Then
      sOutput = "Invalid Data"
      End If
      txtOutput.Text = sOutput
      End Sub
    
Copy and Paste the code below into your Visual C# Project:

      private void pbSwitch_Click(object sender, EventArgs e)
      {
      ProcessSwitch();
      }
      private void ProcessSwitch()
      {
      string sOutput = "";
      int nVal = System.Convert.ToInt32(cmbValue.Text);

      switch (nVal)
      {
      case 1:
      sOutput = "A";
      break;
      case 2:
      sOutput = "B";
      break;
      case 3:
      sOutput = "C";
      break;
      case 4:
      sOutput = "D";
      break;
      case 5:
      sOutput = "E";
      break;
      default:
      sOutput = "Invalid Input";
      break;
      }
      txtOutput.Text = sOutput;
      }
      private void pbIf_Click(object sender, EventArgs e)
      {
      ProcessIf();
      }
      private void ProcessIf()
      {
      string sOutput = "";
      int nVal = System.Convert.ToInt32(cmbValue.Text);

      if (nVal == 1)
      {
      sOutput = "A";
      }
      if (nVal == 2)
      {
      sOutput = "B";
      }
      if (nVal == 3)
      {
      sOutput = "C";
      }
      if (nVal == 4)
      {
      sOutput = "D";
      }
      if (nVal == 5)
      {
      sOutput = "E";
      }
      if (nVal > 5)
      {
      sOutput = "Invalid Data";
      }
      txtOutput.Text = sOutput;
      }
    
You can see that the select case and switch statements are more readable. You can also see the differences between the syntax in Visual Basic and Visual C#. For instance in C#, ‘then’ is not used in an if-then statement, and it is necessary to put opening and closing braces ‘{}’ after ‘if’, and you must place the test condition inside parenthesis. Also when using the switch statement in Visual C#, you must have opening and closing braces ‘{}’, however when using the Select Case statement in Visual Basic, you must have an End Select statement.#CSharp #VisualBasic #VisualStudio
Some words from the sponsors...

Add a C# Dropdown to a Bootstrap Modal Dialog

This example combines C# and Bootstrap to create a bootstrap modal dialog containing an asp dropdown and an asp button to retrieve...

Continue Reading

Use C# to Populate Dropdown Lists With XML

The code below opens an xml file, reads the data from the xml file, dynamically adds a label for the dropdown list, and adds the dropdown list to the asp.net page...

Continue Reading

Add Style Properties to ASP.Net Controls to Improve Appearance

The default formatting for ASP.Net controls is very dated.  The default formatting is basically the same as the formatting in Visual Basic 1.0 ...

Continue Reading

Export To A Text File From A DataGridView Control

Add a button control to Form1.
Add a DataGridView Control to Form1....

Continue Reading