<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DeclaringArrays.aspx.cs" Inherits="tutorials_DeclaringArrays" %> Declaring Arrays Static and Dynamic

Members Library
  C# Video Tutorials
Visual Studio 2008
Intro to VS 2008 Environment
My First Program
Creating Mulit-Project Solutions
Working with Strings
Contains
Length
Stringbuilder
Substring
Trim
Upper Case / Lower Case
IndexOf
Concatenation
Split
String Compare
Manipulating Text Files
Validating User Input
Creating Custom Controls
Using DateTime
Using the Windows API
.NET Forms

In Production
Automate Word with C#
Automate Excel with C#
Create PDF on the fly
Win API Calls
Using the System.Drawing Object
Get image from SQL Server
Upload image to SQL2005 database
Connecting to SQL Server

Coming this week!
Access CD Drive via API
Connecting to SQL Server 2005
Automating Excel with C#
A Custom Rounded Panel Control


JET
TECHNICAL.COM

jet tech
C Sharp Video Tutorials Online
all skill levels
member news
videos
sample code
C# On Demand!

Practical Applications of Arrays

download the source code

Practical Applications of Arrays In Beginners introduction to arrays, we discussed what an array is, the fact that arrays are 0 based (the first place value is referred to with 0, not 1) and that it can contain values in a collection. Next we need to look at practical application for arrays and how to use them efficiently in code.

Static Arrays

Arrays can be declared in many ways. These first examples demonstrate arrays that are created at “design time” – (programmer sets the value(s) and length). This array is “hard coded”. Hard coded means that the values and length of the array are established at the time the array is declared and isn’t based on user input or stored data. It won’t vary while the program is running. Notice that there is more than one way to declare this type of arrays.

string[] strFruitArray = {"apple", "banana", "orange", "grape", "pineapple"};
string[] strFruitArray = new string[]{"apple", "banana", "orange", "grape", "pineapple"};
string[]strFruitArray;
strFruitArray = new string [5] {"apple", "banana", "orange", "grape", "pineapple"};


Regardless of which method is used to declare the array, referencing the individual values is done the same way

strFruitArray[0] = “apple”
strFruitArray[1] = “banana”
strFruitArray[2] = “orange”
strFruitArray[3] = “grape”
strFruitArray[4] = “pineapple”


Dynamic Arrays

Most of the time, we need to have arrays that we won’t know the values or how many items. The next example is an example of a completely dynamic array. The only information set at design time is the data type (int), the variable name (intArray), and that it is an array ([]). The values and the number of values will be based on user input or data retrieved from at runtime. The following is an example of how this type of array is declared.

No length or values set at the time of declaration. Set this at the class level (see full example download) in order for it to be visible to the entire form class.
int[] intArray;

Fixed length at the time of declaration but not the values
intArray = new int[5];

Practical Example of a Dynamic Array

In the example below, there is a list box with some values. When the user clicks the “Create Array” button, the array size is set to the number of items in the list box and a for loop is used to add the values to the dynamic array.

private void btnCreateArray_Click(object sender, EventArgs e)
{
//the number of items in the list box is the size of our array.
int intNumItems = lstBoxValues.Items.Count;//get the number of items
strListItems = new string[intNumItems];

//use a for iteration to add the items.
//List boxes are also 0 based. The first item in the list will be referred to as lstBoxValues[0].
for (int intX = 0; intX < lstBoxValues.Items.Count; intX++)
strListItems[intX] = lstBoxValues.Items[intX].ToString();

//Show array properties in a rich text box named rtbArrayValues
rtbArrayValues.Text = "Total number of elements = " + strListItems.LongLength + "\n" ;//the \n is a return
rtbArrayValues.Text += "The LowerBound() value = " + strListItems.GetLowerBound(0).ToString() + "\n";
rtbArrayValues.Text += "The UpperBound() value = " + strListItems.GetUpperBound(0).ToString() + "\n" ;
//show all of the values using the for iteration
for (int intX = 0; intX <= strListItems.GetUpperBound(0); intX++)
rtbArrayValues.Text += "value " + intX + " is " + strListItems[intX] + "\n";
}
To see it all in action, download the entire project.

Need more help on arrays?
Introduction to Arrays
Using the split method with arrays.


Let us hear your comments, send us an email.
Members Library
  Source Code Downloads
DateTime Functions
Custom Controls
Working with Strings
Working with Text Files
Validating User Input

Free Downloads
Visual Studio Express

Crystal Reports 2008
crystal reports

--MEMBER LOGIN--
email address: password: