ASP.NET
  Home arrow ASP.NET arrow Page 8 - 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 - Jagged Little Pill


    (Page 8 of 8 )

    In all the examples on the previous pages, the arrays have been symmetrical, in that every row of the array has had an equal number of elements. For example, an array defined as

     meals[3,3



    will always hold 9 values, with each row holding an equal number of columns (3). Of course, it is possible that some of these 9 elements might not be assigned any value; in this case, they will hold the default value defined by C# for the data type of that array.

    C# also allows you to define an array that does not have the same number of columns across its rows. It's called a "jagged array", and here's an example:

     <%

    string [][] meals= new string [3][];
    meals [0] = new string[1];
    meals [1] = new string[3];
    meals [2] = new string[2];

    meals[0][0] = "cream of mushroom soup";
        
    meals[1][0] = "pasta";
    meals[1][1] = "lasagne";
    meals[1][2] = "tandoori chicken";
        
    meals[2][0] = "chocolate mousse";
    meals[2][1] = "tiramisu";

    %>



    Since a jagged array itself contains other arrays, it is not surprisingly often referred to as an "array of arrays".

    How about an example? Here's a script that defines a jagged array, assigns values to its elements and displays the values using a "for" loop:


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

        
    // store the names of the courses
        
    string [] courses = {"Starters"," Main Course Dishes""Desserts"};

        
    // define the array
        
    string [][] meals= new string [3][];
        
    meals [0] = new string[1];
        
    meals [1] = new string[3];
        
    meals [2] = new string[2];

        
    // assign values to the elements
        
        // for the first row, starters
        // one column
        
    meals[0][0] = "cream of mushroom soup";
        
            
        
    // for the second row, main courses
        // three columns
        
    meals[1][0] = "pasta";
        
    meals[1][1] = "lasagne";
        
    meals[1][2] = "tandoori chicken";
        
        
        
    // for the third row, desserts
        // two columns
        
    meals[2][0] = "chocolate mousse";
        
    meals[2][1] = "tiramisu";

            
        
    // a "for" loop to iterate through the array
        
    for(int i 0meals.Lengthi++) 
           {
            
    output.Text output.Text "<tr>";
            
    output.Text output.Text "<td><b>" courses[i] + "</b></td>";
            for(
    int j 0meals[i].Lengthj++) 
            {
                
    output.Text output.Text "<td>" meals[i][j] + "</td>";
            }
            
    output.Text output.Text "</tr>";
        }
        

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



    Here's the output.



    The script starts with the definition of the jagged array.

     <%
        
        
    // define the array
        
    string [][] meals= new string [3][];
        
    meals [0] = new string[1];
        
    meals [1] = new string[3];
        
    meals [2] = new string[2];

        
        
    // assign values to the elements
        
        // for the first row, starters
        // one column
        
    meals[0][0] = "cream of mushroom soup";
        
            
        
    // for the second row, main courses
        // three columns
        
    meals[1][0] = "pasta";
        
    meals[1][1] = "lasagne";
        
    meals[1][2] = "tandoori chicken";
        
        
        
    // for the third row, desserts
        // two columns
        
    meals[2][0] = "chocolate mousse";
        
    meals[2][1] = "tiramisu";

        
    // snip

    %>



    Note the manner in which I have assigned values to the various elements. A comparison with previous examples will illustrate the difference between this syntax, and that used for "regular" multi-dimensional arrays.

     <%

    // snip

        // the "for" loop to iterate through the array
        
    for(int i 0meals.Lengthi++) 
        {
            
    output.Text output.Text "<tr>";
            
    output.Text output.Text "<td><b>" courses[i] + "</b></td>";
            for(
    int j 0meals[i].Lengthj++) 
            {
                
    output.Text output.Text "<td>" meals[i][j] + "</td>";
            }
            
    output.Text output.Text "</tr>";
        }
        
    // snip
    %>



    Once again, I have used two "for" loops to iterate over the array. Note my usage of the "Length" property to find the number of elements at each level; this ensures that the script does not attempt to access an element that does not exist.
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    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 4 hosted by Hostway
    Stay green...Green IT