/News

ASP.Net Switch Case Statement

Source:tutorialvault   2009-10-28    Comments:0  Click:
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:

 

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:

01.if (int.Parse(txtInput.Text) == 1)
02.{
03.    lblResult.Text = "ONE is the string for 1";
04.}
05.else if (int.Parse(txtInput.Text) == 2)
06.{
07.    lblResult.Text = "TWO is the string for 2";
08.}
09.else if (int.Parse(txtInput.Text) == 3)
10.{
11.    lblResult.Text = "THREE is the string for 3";
12.}
13.else if (int.Parse(txtInput.Text) == 4)
14.{
15.    lblResult.Text = "FOUR is the string for 4";
16.}
17.else if (int.Parse(txtInput.Text) == 5)
18.{
19.    lblResult.Text = "FIVE is the string for 5";
20.}

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:

01.switch (int.Parse(txtInput.Text))
02.{
03.    case 1:
04.        lblResult.Text = "ONE is the string for 1";
05.        break;
06.    case 2:
07.        lblResult.Text = "TWO is the string for 2";
08.        break;
09.    case 3:
10.        lblResult.Text = "THREE is the string for 3";
11.        break;
12.    case 4:
13.        lblResult.Text = "FOUR is the string for 4";
14.        break;
15.    case 5:
16.        lblResult.Text = "FIVE is the string for 5";
17.        break;
18.}

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:

01.switch (int.Parse(txtInput.Text))
02.{
03.     case 1:
04.         lblResult.Text = "ONE is the string for 1";
05.         break;
06.     case 2:
07.         lblResult.Text = "TWO is the string for 2";
08.         break;
09.     case 3:
10.         lblResult.Text = "THREE is the string for 3";
11.         break;
12.     case 4:
13.         lblResult.Text = "FOUR is the string for 4";
14.         break;
15.     case 5:
16.         lblResult.Text = "FIVE is the string for 5";
17.         break;
18.     default:
19.         lblResult.Text = "You have entered an invalid choice";
20.         break;
21.}

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:

01.switch (int.Parse(txtInput.Text))
02.{
03.    case 1:
04.    case 2:
05.    case 3:
06.        lblResult.Text = "You have entered 3 or less";
07.        break;
08.    case 4:
09.        lblResult.Text = "You have entered 4";
10.        break;
11.}

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.

Computer Tutorials
Download: NVIDIA Forceware 195.39
You can now download NVIDIA GeForce Forceware 195.39 driver for Windows 7 and Vista (32/64-bit)  full story
Review: Sony Ericsson Satio
Sony Ericsson have long ago shown that they are not afraid of any challenge, producing handsets that have claimed a top spot on the market.  full story
Intel 34nm X25-M Gen 2 SSD Performance Update
In all of our recent solid state disk (SSD) coverage that featured one of Intel's X25-M drives, a common, underlying performance trend consistently em  full story
Download:ATI Catalyst 9.10
AMD released the latest version of its ATI Catalyst software suite  full story
Download: MSI AfterBurner 1.30
Afterburner is powered by our own Rivatuner engine, but this software comes with a few extra's.  full story
AMD Athlon II X3 435 CPU Review
Today, when Windows 7 OS launch is getting closer by day, AMD is rolling out quite a few new processors.   full story
Download: Realtek HD Audio 2.35
The latest download of Realtek's HD audio driver version available for Windows XP and Vista, it runs under Windows 7 too  full story
Home  |  Notebook  |  Bios  |  Display  |  Sound  |  Printer  |  Dell drivers  |  HP drivers  |  Intel  |  Apple
Sony  |  Canon  |  Software  |  PDA  |  News  |  Forum
?2009 Driversdown.com. All Rights Reserved.