TutorialsRoom.com
RSS Feed

Subscribe: RSS or e-mail

Home » Web Development » ASP.NET »

ASP.Net Switch Case Statement

ASP.Net Switch Case Statement
Today we will be talking about ASP.Net switch case statement in C#, when to use the if statement and when to use the switch case statement and the differences between C# switch case statement, the VB.Net select case statement and C++ switch case statement.

Start by opening the previous if statement project or simply drop two labels, one textbox and one button on the form, name one label lblResult, the button btnExecute and the textbox txtInput.

Change the text and layout of your controls on the page so they look like the following:

Asp.Net Properties Window.

Now, suppose we want to use if statement to change what we display according to what the user enters, here is the code that we will be writing:

	if (int.Parse(txtInput.Text) == 1)
	{
		lblResult.Text = "ONE is the string for 1";
	}
	else if (int.Parse(txtInput.Text) == 2)
	{
		lblResult.Text = "TWO is the string for 2";
	}
	else if (int.Parse(txtInput.Text) == 3)
	{
		lblResult.Text = "THREE is the string for 3";
	}
	else if (int.Parse(txtInput.Text) == 4)
	{
		lblResult.Text = "FOUR is the string for 4";
	}
	else if (int.Parse(txtInput.Text) == 5)
	{
		lblResult.Text = "FIVE is the string for 5";
	}
	

Although this code will work fine, this is not the best way to do it. If you are using many else if statements as in the previous example, the code will be more readable if you choose to use the switch case statement instead. Let's convert the previous code to switch case statement, start by typing switch then press the TAB key twice to invoke the Code Snippet of the switch statement in visual studio then convert your code like that:

        switch (int.Parse(txtInput.Text))
        {
            case 1:
                lblResult.Text = "ONE is the string for 1";
                break;
            case 2:
                lblResult.Text = "TWO is the string for 2";
                break;
            case 3:
                lblResult.Text = "THREE is the string for 3";
                break;
            case 4:
                lblResult.Text = "FOUR is the string for 4";
                break;
            case 5:
                lblResult.Text = "FIVE is the string for 5";
                break;
        }
      

The code is now more readable don't you agree? Let's explain the code, in switch statement, you type what the switch should test for after the switch keyword - It could be either integer or string - then you type as many case blocks as you need. But you cannot have duplicate values after the case keywords, also, you cannot check for ranges in C# switch statements like you do in VB.Net select case statements.

Now, what if you enter value less than one or more than five? Here comes the default keyword which will execute the code if no case keywords values were found, modify your code like that:

           switch (int.Parse(txtInput.Text))
           {
                case 1:
                    lblResult.Text = "ONE is the string for 1";
                    break;
                case 2:
                    lblResult.Text = "TWO is the string for 2";
                    break;
                case 3:
                    lblResult.Text = "THREE is the string for 3";
                    break;
                case 4:
                    lblResult.Text = "FOUR is the string for 4";
                    break;
                case 5:
                    lblResult.Text = "FIVE is the string for 5";
                    break;
                default:
                    lblResult.Text = "You have entered an invalid choice";
                    break;
           }
      

Notice that, unlike C++, you have to type break after every case block or you will get the "Control cannot fall through from one case label ('label') to another" while in C++ it will test for other case statements if you don't type the break keyword after the case block. One exception for that is to have case statement with no code like that:

            switch (int.Parse(txtInput.Text))
            {
                case 1:
                case 2:
                case 3:
                    lblResult.Text = "You have entered 3 or less";
                    break;
                case 4:
                    lblResult.Text = "You have entered 4";
                    break;
            }
      

You can use the goto keyword after the case block and ASP.Net will execute the code in the other case statements like that "goto case 2", also you can use the return keyword any where in the case block to terminate the switch statement.

Rating: 2.9/5 (106 Votes)
Copyright © 2007-2014 Hazem Osman. All rights reserved. Terms & Conditions