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.
 

This free website was made using Yola.

No HTML skills required. Build your website in minutes.

Go to www.yola.com and sign up today!

Make a free website with Yola