Showing posts with label Multidimetional String Array. Show all posts
Showing posts with label Multidimetional String Array. Show all posts

Monday, February 18, 2008

Multidimensional Array in C#

Showing a simple example of a multidimensional array in c# because people coming from c++ background and all find it very difficult to make a multidimensional array in c#.

Here I am using a 2 dimensional array whose size is given in two textboxes,and later the values in the array is displayed in the listbox in the same form.

private void _addToArrayButton_Click(object sender, EventArgs e)
{
int total = 0;
int x, y = 0;
x = int.Parse(_firstPosText.Text);
y = int.Parse(_secondPosText.Text);
string[,] mulArray = new string[x, y];
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
total = i + j;
mulArray[i, j] = total.ToString();
}
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
_arrayList.Items.Add(mulArray[i, j].ToString());
}
}
}

A simple small example just to show the input to and output from a multidimensional array.