ASP.NET
  Home arrow ASP.NET arrow Page 7 - 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 
Moblin 
JMSL Numerical Library 
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


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


    (Page 7 of 8 )

    So far, I have used the "for" loop to navigate through the contents of an array. But there's more than one way to skin a cat...and so, C# also lets you iterate over an array with a new loop construct, one I conveniently avoided explaining last time. It's called a "foreach" loop, and it looks like this:

     foreach (element in array) { do this! } 



    Here is a simple example for the "foreach" loop.


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

        
    // define the type of array
        
    int [] lucky_numbers;
        
        
    // initialize array
        
    lucky_numbers = new int[5];
        
        
    // assign values to each element
        
    lucky_numbers[0] = 3;
        
    lucky_numbers[1] = 6;
        
    lucky_numbers[2] = 9;
        
    lucky_numbers[3] = 21;
        
    lucky_numbers[4] = 27;
        
        
    // use the foreach loop
        // to iterate over each element
        
    foreach(int lucky_number in lucky_numbers
           {
            
    output.Text output.Text "  <b>" lucky_number "</b>";
        }
        

    </SCRIPT>
     Here are my lucky numbers: <asp:label id=output runat="server"></asp:label>. What are yours? 



    This is what you should see in our browser.



    Here, I've first defined an array of integers called "lucky_numbers", to store a set of numbers. Next, I've used the "foreach" loop to iterate over the loop and print each element of the array.

     <%
    // snip 

    // use the foreach loop
    // to iterate over each element
    foreach(int lucky_number in lucky_numbers
    {
        
    output.Text output.Text "  <b>" lucky_number "</b>"; }

    // snip 
    %>



    Each time the loop runs, it stores the current element of the array in the "lucky_number" instance variable. Once the loop runs out of its elements to process, it automatically exits and control passes to the line following it.

    How about iterating over a two-dimensional array? Take a look:


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

         
    // define an array
         
    string [,] meals
         
         
    // initialize array with two dimensions
         
    meals = new string[3,3];
              
          
    // assign values to each element
          
    meals[0,0] = "vegetable soup";
          
    meals[0,1] = "cream of mushroom soup";
          
    meals[0,2] = "chicken soup";
          
    meals[1,0] = "lasagna";
          
    meals[1,1] = "pasta";
          
    meals[1,2] = "tandoori chicken";
          
    meals[2,0] = "chocolate mousse";
          
    meals[2,1] = "tiramisu";
          
    meals[2,2] = "apple pie";
     
         foreach(
    string dish in meals
           {
            
    output.Text output.Text "  " dish;
        }



    </SCRIPT>
     <asp:label id=output runat="server"></asp:label>. 



    This is the output.



    As you can see, the "foreach" loop does not give me as much control over the array, as compared to the two "for" loops demonstrated in the earlier example.

    Thus far, the number of columns in the array was equal to the number of rows. However, this is not a hard and fast rule - it is possible to define a multi-dimensional array which is not so symmetrical in its dimensions, such as

     books[10,4]; 



    or

     authors[30,5



    However, using an array with three or more dimensions leads to complex programming logic, and code that is difficult to write, read and debug; it should therefore be avoided as far as possible.

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


     

    ASP.NET ARTICLES

    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - 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...





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