ASP.NET Code
  Home arrow ASP.NET Code arrow Page 4 - 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 
IBM Developerworks
 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

    Free Web 2.0 Code Generator! Generate data entry 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 - Cookie-Cutter Code
    (Page 4 of 8 )

    The "if-else" construct certainly offers a smidgen more flexibility than the basic "if" construct, but still limits you to only two possible courses of action. If your script needs to be capable of handling more than two possibilities, you should reach for the "if-else if-else" construct, which is a happy combination of the two constructs you've just been reading about.


    if (first condition is true)
    {
    do 
    this!
    }
    else if (
    second condition is true)
    {
    do 
    this!
    }
    else if (
    third condition is true)
    {
    do 
    this!
    }

    ... and 
    so on ...

    else
    {
    do 
    this!
    }

    Take a look at it in action:


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

    // temperature
    int temp 5;

    if(
    temp >= 35
    {
    message.Text "Man, it's hot out there!";

    else if (
    temp 35 && temp >= 10
    {
    message.Text "Well, at least it isn't as hot as it could be!";
    }
    else if (
    temp 10
    {
    message.Text "Man, it's freezing out there!";
    }
    // this is redundant, included for illustrative purposes
    else 
    {
    message.Text "Huh? Somebody screwed up out there!";
    }


    </script>
    <html>
    <head><title>Weather Forecast</title></head>
    <body>
    <asp:label id="message" runat="server" />
    </body>
    </html>

    In this case, depending on the value of the "temp" variable, the appropriate code branch is executed, thereby making it possible to write scripts, which allow for multiple possibilities.

    One important point to be noted here: control is transferred to successive "if" branches only if the preceding condition(s) turn out to be false. Or, in English, once a specific conditional expression is satisfied, all subsequent conditional expressions are ignored.

    Here's another example, this one using the day of the week to decide which fortune cookie to display.


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

    // get the current day of the week
    DateTime date System.DateTime.Now;

    string day date.DayOfWeek.ToString();

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

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

    </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:

    A quick note on the following lines of code:


    // snip

    DateTime date System.DateTime.Now;
    string day date.DayOfWeek.ToString();

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

    // snip

    That was an off-the-cuff intro to the DateTime data type in C#. I shall talk about this little data type at a later date (pun intended!). For the moment, just assume that the above code snippet returns the current day of the week.

    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 5 hosted by Hostway