ASP.NET
  Home arrow ASP.NET arrow ASP.NET Basics (Part 5): Cooking Up a Stor...
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 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

ASP.NET Basics (Part 5): Cooking Up a Storm
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2003-11-03

    Table of Contents:
  • ASP.NET Basics (Part 5): Cooking Up a Storm
  • Over and Out
  • Deeper Waters
  • What's for Dessert?
  • Changing Things Around
  • A Full Meal
  • Getting Lucky
  • Jagged Little Pill

  • 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
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    ASP.NET Basics (Part 5): Cooking Up a Storm


    (Page 1 of 8 )

    asp.netLearn all about arrays, enumerations, and the "foreach" loop, and find out how you can use them for more complex data storage and manipulation. Harish Kamath takes us through a tour of ASP.NET in this fifth of a nine part series. Here's a clip: "Another interesting type of complex variable in C# is the so-called enumeration, or 'enum', type, which allows you to restrict the value of a variable to a list of pre-defined values."Another interesting type of complex variable in C# is the so-called enumeration, or "enum", type, which allows you to restrict the value of a variable to a list of pre-defined values. Here's what it looks like:

     enum variable-name {list of values



    And here's an example:

     <%
    enum Months JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember }; %>



    In order to use the enumeration, you need to define a variable of that type and assign it a value, as below.

     <%
    Months myBirthMonth Months.July;
    %>



    Try as you might, C# will not allow you to assign a other value other than those listed in the enumeration to the variable "myBirthMonth".

    The next example demonstrates the use of the "Months" enumeration in a simple C# script.


    <SCRIPT language=c# runat="server">



    enum Summer 
    April 4May 5June 6July 7};



    void Page_Load
    () 
    {   

        
    DateTime date System.DateTime.Now;
        
        
    Summer current_month;

        
    current_month = (Summerdate.Month;
        
        if (
    Enum.IsDefined(typeof(Summer), current_month)) 
        {
            
    output.Text "Summer time! Get to work on that tan!"
        } 
        else 
        {
            
    output.Text "It ain't summer, bud, so no point stocking up on suntan lotion"
        }
        

    </SCRIPT>
     <?xml:namespace prefix asp /><asp:label id=output runat="server"></asp:label>



    Here is what the output looks like if it's summer,



    and here's what it looks like when it's not.



    In this example, I've used a "Summer" enumeration to create a set of values representing the four summer months of the year.

    Typically, each value in an "enum" is associated with an integer. By default, the compiler will automatically assign an integer value, starting with 0 for the first value. But this does not really suit my purposes on two counts: first, there is no such thing as a 0th month and second, I need the month to be assigned a number corresponding to its correct position in the calendar. Therefore, I have explicitly defined integer values for each month.

     <%
    enum Summer April 4May 5June 6July 7};
    %>



    Next, the current month (an integer value, by default) is obtained from the system, and cast into the "Summer" enum data type.

     <%
    DateTime date System.DateTime.Now;
    Summer current_month;
    current_month = (Summerdate.Month;
    %>



    Finally, the IsDefined() method of the "Enum" object is used to check if the value assigned to the "current_month" variable exists in the definition for the "Summer" enumeration. Don't worry too much about this, I'll be discussing objects and methods a little later on in this series. For the moment, all you need to know is that the IsDefined() method returns true if the value exists in the enumeration, and false if it does not; the output will change accordingly.

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


     

    ASP.NET ARTICLES

    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...
    - Back-end Management Tasks for an ASP.NET AJA...
    - User Information Management for an ASP.NET A...
    - Adding Comments and Search to an ASP.NET AJA...
    - Order-Related Modules for an ASP.NET AJAX Se...
    - User and Role Management for an ASP.NET AJAX...
    - Programming an ASP.NET AJAX Server-Centric B...





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