469,362 Members | 2,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

how to return an Array ?

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
Feb 20 '06 #1
6 50854
did you try string[,] ?
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Andrew" <An****@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
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

Feb 20 '06 #2
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

"Andrew" <An****@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
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

Feb 20 '06 #3
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" <An****@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
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

Feb 20 '06 #4
Andrew <An****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 20 '06 #5
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" wrote:
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" <An****@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
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


Feb 21 '06 #6
Andrew wrote:
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

Feb 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by the gullers | last post: by
16 posts views Thread by priya | last post: by
2 posts views Thread by kathy | last post: by
2 posts views Thread by Ali | last post: by
5 posts views Thread by Sam | last post: by
reply views Thread by anthony | last post: by
5 posts views Thread by Igoogler | last post: by
1 post views Thread by =?Utf-8?B?RGFya3Jpc2Vy?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.