Logo         Community
The Company
Your Account
Contact Us
ASP.NET Basics (part 5): Cooking Up A Storm
Learn all about arrays and enumerations, and use them for more complex data storage and manipulation.

| What's For Dessert? |

Thus far, the variables you've used contain only a single value - for example,

int i = 0

However, array variables are a different kettle of fish altogether. An array is a complex variable that allows you to store multiple values in a single variable; it comes in handy when you need to store and represent related information. An array variable can, thus, best be thought of as a "container" variable, which can contain one or more values. For example,

string [] desserts = {"chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "caramel custard"};

Here, "desserts" is an array variable, which contains the values "chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", and "caramel custard". It is thus clear that array variables are particularly useful for grouping related values together - names, dates, phone numbers of ex-girlfriends et al.

The definition shown above is a shorthand method to define an array. The proper syntax to define an array in C# is shown below.

// define the type of array
string [] desserts;

// initialize array
// the number indicates the number of elements the array will hold
desserts = new string[5];

// assign values to each element
// of the "desserts" array defined above
desserts[0] = "chocolate mousse";
desserts[1] = "tiramisu";
desserts[2] = "apple pie";
desserts[3] = "chocolate fudge cake";
desserts[4] = "caramel custard";

Defining an array variable in C# is a multi-step process, as compared to languages like PHP and Perl; you need to first declare the variable and its type, and then actually create the array.

The various elements of the array are accessed via an index number, with the first element starting at zero. So, to access the element

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
chocolate mousse

you would use the notation



''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
"chocolate fudge cake"

would be


- essentially, the array variable name followed by the index number enclosed within square braces. Notice that the first element of the array is referred to by index number 0 instead of 1; geeks refer to this as "zero-based indexing".

Unlike less strict programming languages like PHP and Perl, C# arrays can only store data of the type specified at the time of declaring the array variable - a string array cannot hold numbers, or vice-versa. So, if you were to try the following,

string [] desserts = new string[5];

// assign an integer value
desserts[0] = 100;

C# would fail with an ugly type conversion error message.

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback