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.

Advertisements
About

Over 20 years programming experience 5 years Oracle SQL/Stored Procedure programming Over 10 years SQL 2000/2005/2008 Server SQL/Stored Procedure programming Over 18 years Visual Basic 3.0/4.0/5.0/6.0/.Net/2003/2005/2008/2010/2015 Over 10 years ASP.Net Over 7 years Visual C# Over 20 years Microsoft Office and VBA HTML JAVA COBOL EASYTRIEVE FORTRAN

Tagged with: , , , , ,
Posted in Visual Studio Code Examples

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: