Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Working with Arrays Using C#

Author: Devasp
Download Source Code : 672_Arrays.zip

A simple article that shows you, how u can use single dimension and double dimension array in C# and windows form.

To begin with add textbox, listbox and button controls on your windows form. Now start adding methods on code behind. First create a PrintSingleDimArray(int[] arr) code for this method is as follows:

public void PrintSingleDimArray(int[] arr)

{

 

     for (int i = 0; i < arr.Length; i++)

 

     {

 

       lbSingleArray.Items.Add("Result for singleDim array rows" + i + " " + arr[i]);

 

     }

 

}

Note this method take a single dimension array as an argument. Now create another method named as PrintDoubleDimArray(int[,] arr, int loop1, int loop2) code for this method is as follows:

 

public void PrintDoubleDimArray(int[,] arr, int loop1, int loop2)

{

           

for (int i = 0; i < loop1; i++)

{

         for (int j = 0; j < loop2; j++)

         {

     

     arr[i, j] = i * j;

 

           lbDoubleArray.Items.Add("Double dim array...row " + i + " Column " + j + " Array Val  " + arr[i,j]);

 

         }

 

     }

 

 }      

 

Note this method takes three parameters first is array second and third parameters the number of time loop will iterate. In above two methods you will also see that results are being add in listbox control.

 

Now add the Click events on your windows form. Code for these events is as follows:

 

        private void button1_Click(object sender, EventArgs e)

        {

            int txt1 = Convert.ToInt16(textBox1.Text);

 

            int[] arr1 = new int[txt1];

 

            this.PrintSingleDimArray(arr1);

        }

 

 

 

        private void button2_Click(object sender, EventArgs e)

        {

            int txt1 = Convert.ToInt16(textBox1.Text);

            int txt2 = Convert.ToInt16(textBox2.Text);

 

            int[,] arr2 = new int[txt1, txt2];

 

            this.PrintDoubleDimArray(arr2, txt1, txt2);

        }

 

These events on their will display results of arrays in respective list box controls. (Sample code is also attached with this article).

Article Comments
you are teaching asp.net but yourself on html.....
html is good or asp.net..
.net is advance..
jscript is at its own..no comparison????

Posted on 3/25/2007 1:29:39 AM by SeekHack

Thanks for the info.

Posted on 11/19/2009 1:19:41 AM by catherine

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Customize GridView Cells in .NET 2.0 using C#

Disclaimer - Privacy
© 2002-2017 DevASP.net