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 soup
, cream of mushroom soup, chicken soup Main course lasagna, pasta, tandoori chicken Dessert chocolate mousse, tiramisu, apple 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:
[code] meals[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 = 3, columns = 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 = 0; i < rows; i++)
{
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 = 0; j < columns; j++)
{
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 = 0; i < rows; i++)
{
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 = 0; j < columns; j++)
{
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.
Next: Getting Lucky >>
More ASP.NET Articles
More By Harish Kamath (c) Melonfire