TutorialsRoom.com, Where knowledge lands
RSS Feed

Subscribe: RSS or e-mail

Home » Web Development » ASP.NET »

ASP.Net Switch Case Statement

bitmap_vector (1K)
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: 3.1/5 (64 votes)
10 Comments
Post A Comment
ar Said,
Mon, 28 December 2009 12:12pm (GMT)

good

kushal singla Said,
Wed, 10 February 2010 16:26pm (GMT)

its nice tutorial helped me to correct my mistake keep up your good work

Karma Said,
Fri, 05 March 2010 07:13am (GMT)

Thanks for the post.It was really helpful.

Anurag Said,
Wed, 14 July 2010 09:09am (GMT)

exactly what I was looking for... thanks

vivek Said,
Sat, 17 July 2010 01:27am (GMT)

how to use switch in asp.net in vbscript

Kumar Said,
Tue, 25 January 2011 18:04pm (GMT)

Thanks!It help me out

kulwidner sarao Said,
Tue, 15 February 2011 03:43am (GMT)

it is good thing which help me a lot

Thu, 28 April 2011 09:44am (GMT)

Thanks A LOT!!! It helps me in a great way!! :)

Daniel Said,
Tue, 31 May 2011 08:50am (GMT)

Thanks,by this i have corrected my mistake.

Jasmine Said,
Fri, 02 September 2011 08:03am (GMT)

Great simple tutorial on ASP.net select case. I am new to ASP.net and your tutorial has help me a lot. Thanks, mate.

Post Your Comment: (English Only Please)


(Required)


(Optional, will not be shown)


(Including http:// - Optional)



Type the sum of 5 + 3 (Required)

Copyright © 2007-2012 Hazem Osman. All rights reserved. Terms & Conditions