ASP.NET
  Home arrow ASP.NET arrow Page 6 - 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 
Mobile Linux 
App Generation ROI 
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 - A Full Meal


    (Page 6 of 8 )

    As explained on the previous page, an array makes it possible to store a group of logically-related values - in this case, a list of mouth-watering desserts. However, there's more to a meal than just the desserts; the desserts are usually preceded by the starters and the main courses (more than one if the cook is a culinary enthusiast).

    Now, if I wanted to, I could create an array to store the entire set of items for a meal, as below:

     <%
    string [] meals = {"vegetable soup""cream of mushroom soup""chicken soup""lasagna""pasta""tandoori chicken""chocolate mousse""tiramisu""apple pie" }; %>



    The only problem here? There's no way to distinguish between starters, main courses and desserts. What is really needed is a variable that stores all the values, yet provides some way to distinguish between them, as below:

     Starter vegetable soupcream of mushroom soupchicken soup Main course lasagnapastatandoori chicken Dessert chocolate moussetiramisuapple pie 



    Is there a way to do this in C#? Sure, with a multi-dimensional array.

    Thus far, the arrays that you've seen have been one-dimensional arrays; they store values in a single column. However, C# also permits two-dimensional arrays, which allow you to store data in rows and columns. Here's an example:

     <%

    // define the type of array
    string [,] meals;

    // initialize array with two dimensions
    meals = new string[3,4];

    // 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";

    %>



    Here the index is made up two values - the first represents the row and the second represents the column. If you look closely, you'll see that the first row stores the starters, the second stores the main course dishes and the last contains the desserts.

    So, to access the element

     chocolate mousse [code

    you would 
    use the notation

    [codemeals[2,0



    Note the use of two values in the index to access the element: the first value represents the "row" and the second represents the "column".

    Let's now put together a simple script to output the menu in a tabular format:


    <SCRIPT language=C# runat="server">
    void Page_Load()
    {   
        
    // store the names of the courses
        
    string [] courses = {"Starters"," Main Course Dishes""Desserts"};
        
        
    // variables to store the number of rows and columns
        
    int rows 3columns 3;

        
    // define the type of array
        
    string [,] meals;

        
    // initialize array with two dimensions
        
    meals = new string[rows,columns];

        
    // assign values to each element

        // Starters in row #1
        
    meals[0,0] = "vegetable soup";
        
    meals[0,1] = "cream of mushroom soup";
        
    meals[0,2] = "chicken soup";

        
    // Main Course Dishes in row #2
        
    meals[1,0] = "lasagna";
        
    meals[1,1] = "pasta";
        
    meals[1,2] = "tandoori chicken";

    // Desserts in row #3
        
    meals[2,0] = "chocolate mousse";
        
    meals[2,1] = "tiramisu";
        
    meals[2,2] = "apple pie";

        
    // the outer "for" loop to access each row
        
    for(int i 0rowsi++) 
        {

            
    output.Text output.Text "<tr>";
            
    output.Text output.Text "<td><b>" courses[i] + "</b></td>";

            
    // the inner "for" loop to access each column
            
    for(int j 0columnsj++) 
                     {
            
    output.Text output.Text "<td>" meals[i,j] + "</td>";
            }

            
    output.Text output.Text "</tr>";
        }
        

    </SCRIPT>
     
    <TABLE cellSpacing=0 cellPadding=5 width="75%" border=1><asp:label id=output runat="server"></asp:label>
    <TBODY></TBODY></TABLE>



    And here's the output.



    As usual, I start the script by defining some variables - two integers to store the number of rows and columns for the two-dimensional array, followed by the arrays themselves. The first array, "courses", stores the names of the three courses, while the second, "meals", actually stores the values for each.

    Next, I have the most crucial part of the code: the two "for" loops used to iterate through the "meals" array. This deserves closer attention.

     <%
        
    // snip 

        // the outer "for" loop to access each row
        
    for(int i 0rowsi++) 
        {

            
    output.Text output.Text "<tr>";
            
    output.Text output.Text "<td><b>" courses[i] + "</b></td>";

            
    // the inner "for" loop to access each column
            
    for(int j 0columnsj++) 
            {

                
    output.Text output.Text "<td>" meals[i,j] + "</td>";
            }
            
    output.Text output.Text "</tr>";
        }

        
    // snip 
    %>



    As you can see, the first "for" loop iterates through the rows of the array, with the variable "i" keeping track of the first dimension. Within this outer loop is another "for" loop, used to access each column of the array using the variable "j". Combined, these two loops make it possible to access each and every element of the two-dimensional array, by iterating across the length and breadth of the array.

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


     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - 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

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT