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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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


    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

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek