how to return an Array ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I have a method that wants to return an array. What datatype should I use ?
I tried using an "Array" but there was a compilation error. Why ?

public Array setReviewAll2 (int intNumQn)
int rows = intNumQn;
int cols = 3;
string [,] arrRv = new string [rows,cols];
for ( int i = arrRv.GetLowerBound(0); i <= arrRv.GetUpperBound(0); i++ ){
for ( int j = arrRv.GetLowerBound(1); j <= arrRv.GetUpperBound(1); j++ )
{
...
...
...
}
}
arrRv.SetValue( strAnswer, i, j );
return arrRv;
}

TIA.

regards,
Andrew
 
Hi,

Hope the following will answer your question


private void button1_Click(object sender, EventArgs e)
{

string[] b=tes();

// Out put hi hello
MessageBox.Show(b[0].ToString() + ' ' + b[1].ToString() ) ;


}

private string[] tes()
{
string[] a = new string[2];
a[0] = "hi";
a[1] = "hello";

return a;
}


Vinu
 
Hi,

Hope this might be use full... this is how to return a Multidimensional
array from a function...

private void button1_Click(object sender, EventArgs e)
{

string[,] b=tes();
MessageBox.Show(b[1,1].ToString());


}

private string[,] tes()
{
string[,] siblings = new string[2, 2] { { "Mike", "Amy" }, {
"Mary", "Albert" } };
return siblings;
}

Vinu
 
Andrew said:
I have a method that wants to return an array. What datatype should I use ?
I tried using an "Array" but there was a compilation error. Why ?

public Array setReviewAll2 (int intNumQn)
int rows = intNumQn;
int cols = 3;
string [,] arrRv = new string [rows,cols];
for ( int i = arrRv.GetLowerBound(0); i <= arrRv.GetUpperBound(0); i++ ){
for ( int j = arrRv.GetLowerBound(1); j <= arrRv.GetUpperBound(1); j++ )
{
...
...
...
}
}
arrRv.SetValue( strAnswer, i, j );
return arrRv;
}

Your compilation error is due to you using i and j outside the loop, I
suspect.

Note that as you know the lower and upper bounds anyway, and because C#
knows the type of arrRv to be string[,] you can make your code a lot
simpler:

for (int i=0; i < rows; i++)
{
for (int j=0; j < cols; j++)
{
arrRv[i,j] = ...;
}
}

A string[,] return type would generally be preferable though.
 
Hi guys,

Thanks for your replies. Yup, string [,] works fine.
I was wondering if there is a difference between string[][] and string[,] ??
Also I have since decided not to use a multidimensional array in this
instance coz webservices dun support multi-dimensional arrays.

regards,
andrew

vinu said:
Hi,

Hope this might be use full... this is how to return a Multidimensional
array from a function...

private void button1_Click(object sender, EventArgs e)
{

string[,] b=tes();
MessageBox.Show(b[1,1].ToString());


}

private string[,] tes()
{
string[,] siblings = new string[2, 2] { { "Mike", "Amy" }, {
"Mary", "Albert" } };
return siblings;
}

Vinu



Andrew said:
Hi all,

I have a method that wants to return an array. What datatype should I use
?
I tried using an "Array" but there was a compilation error. Why ?

public Array setReviewAll2 (int intNumQn)
int rows = intNumQn;
int cols = 3;
string [,] arrRv = new string [rows,cols];
for ( int i = arrRv.GetLowerBound(0); i <= arrRv.GetUpperBound(0); i++ ){
for ( int j = arrRv.GetLowerBound(1); j <= arrRv.GetUpperBound(1);
j++ )
{
...
...
...
}
}
arrRv.SetValue( strAnswer, i, j );
return arrRv;
}

TIA.

regards,
Andrew
 
Andrew said:
Thanks for your replies. Yup, string [,] works fine.
I was wondering if there is a difference between string[][] and string[,] ??

Yes - string[][] is an array of arrays of strings, and a string[,] is a
genuine multidimensional array. You should search for "jagged array"
and "rectangular array" for more details.
Also I have since decided not to use a multidimensional array in this
instance coz webservices dun support multi-dimensional arrays.

I don't know the details of that, but I wouldn't be surprised.

Jon
 
Back
Top