ASP.NET Code
  Home arrow ASP.NET Code arrow Page 7 - ASP.NET Basics (part 3): Hard Choices
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET CODE

ASP.NET Basics (part 3): Hard Choices
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 29
    2003-08-19

    Table of Contents:
  • ASP.NET Basics (part 3): Hard Choices
  • Apples And Oranges
  • Do It Or Else...
  • Cookie-Cutter Code
  • Three In One
  • Friday The 13th
  • Switching Things Around
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    ASP.NET Basics (part 3): Hard Choices - Switching Things Around
    (Page 7 of 8 )

    Finally, C# rounds out its basket of conditional expressions with the "switch" statement, which offers an alternative method of transferring control from one program block to another. Here's what it looks like:


    switch (decision-variable)
    {
    case 
    first-condition:
    // do this!
    break;

    case 
    second-condition:
    // do this!
    break;

    case 
    third-condition:
    // do this!
    break;

    // ... and so on...

    default:
    // do this by default!
    break;
    }

    The "switch" statement can best be demonstrated by rewriting the previous example using "switch" instead of "if-else if-else".


    <script language="c#" runat="server">
    void Page_Load()

    DateTime date System.DateTime.Now;

    string day date.DayOfWeek.ToString();

    // set the day of the week label
    dayoftheweek.Text day;

    switch(
    day) {
    case 
    "Monday":
    fortune.Text "Adam met Eve and turned over a new leaf.";
    break;
    case 
    "Tuesday":
    fortune.Text "Always go to other people's funerals, otherwise they won't come to yours.";
    break;
    case 
    "Wednesday":
    fortune.Text "An unbreakable toy is useful for breaking other toys.";
    break;
    case 
    "Thursday":
    fortune.Text "Be alert - the world needs more lerts.";
    break;
    case 
    "Friday":
    fortune.Text "Crime doesn't pay, but the hours are good.";
    break;
    default:
    fortune.Text "Sorry, closed on the weekend.";
    break;



    </script>
    <html>
    <head><title>Fortune Cookies, Anyone?</title></head>
    <body>
    Today is <asp:label id="dayoftheweek" runat="server" /> and your fortune cookie says <asp:label id="fortune" runat="server" /> </body> </html> 

    Here's the output:

    The "switch" statement is followed by a decision variable, which serves as the foundation of the construct. Depending on the value of the decision variable, the various "case" blocks are evaluated and the one matching the decision variable (if available) is executed.

    The "switch" statement is followed by a list of "case" blocks, which sets up the different code branches for each possible value of the conditional expression embedded in the "switch" statement. Each block begins with the keyword "case" and a constant value. The "break" keyword is used to break out of the "switch" statement block and move immediately to the lines following it.

    It's interesting also to note that the decision variable at the beginning of the "switch" statement can evaluate different data types such as integers, strings, chars or even Booleans (remember these from the previous article?). A "boolean" data type can have only two values, true or false, and are declared using the "bool" keyword. However, if you have only two options available, it makes more sense to use the vanilla "if-else" statement or the ? conditional operator.

    You can also evaluate multiple possible values within the same "case" block, simply by clubbing them together. This variant demonstrates, by having one "case" block execute for Mondays, Wednesdays and Fridays and a second "case" block for Tuesdays and Thursdays:


    <script language="c#" runat="server">
    void Page_Load()

    DateTime date System.DateTime.Now;

    string day date.DayOfWeek.ToString();

    // set the day of the week label
    dayoftheweek.Text day;


    switch(
    day) {
    case 
    "Monday":
    case 
    "Wednesday":
    case 
    "Friday":
    fortune.Text "Adam met Eve and turned over a new leaf.";
    break;
    case 
    "Tuesday":
    case 
    "Thursday":
    fortune.Text "Always go to other people's funerals, otherwise they won't come to yours.";
    break;

    default:
    fortune.Text "Sorry, closed on the weekend";
    break;



    </script>
    <html>
    <head><title>Fortune Cookies, Anyone?</title></head>
    <body>
    Today is <asp:label id="dayoftheweek" runat="server" /> and your fortune cookie says "<asp:label id="fortune" runat="server" />". </body> </html> 

    A number of other keywords are available in the context of a "switch" conditional statement:

    * The "break" keyword is used to break out of the "switch" statement block and move immediately to the lines following it.

    * The "default" keyword is used to execute a default set of statements when the variable passed to "switch" does not satisfy any of the conditions listed within the block.

    * The "continue" keyword can be used to move program execution back to the beginning of the "switch" block (not recommended as you will end up in a messy endless loop if you're careless)

    * The "goto case" construct can be used to jump to another "case" block, while the "goto default" keyword moves program execution to the default "case" block.

    More ASP.NET Code Articles
    More By Harish Kamath (c) Melonfire


     

    ASP.NET CODE ARTICLES

    - How to Use the ListBox Control in ASP.NET 2.0
    - How to Load XML Documents in ASP.NET 2.0
    - DataGrid Code
    - ASP.NET Guestbook
    - User Controls and Client Side Scripting
    - ASP.NET Programming with Microsoft's AS...
    - ASP.NET Basics (part 3): Hard Choices
    - ASP.NET Basics (part 2): Not My Type
    - ASP.NET Basics (part 1): Nothing But .Net
    - Directory Tree Browser
    - How to get the confirmation of Yes/No from a...
    - Complete example using custom errors and wri...
    - Paging Certain # records per page .NET style
    - General Methods of formatting and Subtractin...
    - .NET LinkButton web control




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway