Click to See Complete Forum and Search --> : Return array from method


Westerberg
October 25th, 2006, 10:24 AM
Is it possible to pass an array from a method?

An example below:


array GetArray()
{
SqlConnection conn = new SqlConnection("Server=127.0.0.1;User ID=me;Pwd=dude;database=mine;);
SqlDataReader rdr = null;
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Table", conn);
rdr = cmd.ExecuteReader();
string[,] names = new string[cnt,6]; //creates a two dimensional array
rdr.Read(); //initial read
for (int i=1; i < cnt; i++)
{
for (int j=1; j < 6; j++)
{
names[i,j] = rdr[j].ToString();
}
rdr.Read();
}
conn.Close();
return names
}

wildfrog
October 25th, 2006, 10:54 AM
Is it possible to pass an array from a method?

yourType[] function()
{
// create array
yourType[] arr = ...;

// return array
return arr;
}

- petter

Westerberg
October 25th, 2006, 11:20 AM
Thanks.

Skoons
October 25th, 2006, 07:03 PM
Well there is the other way, but it`s used in a little diferrent situation that you describes. You can use it to send an array to function to change it and have it back

void SomeFunc(out type [] array)
{
//some actions with array
}

cjard
October 26th, 2006, 10:13 AM
noooooo.. dont tell people that when they havent got the basics understood.. :)

out and ref parameter modifiers should be left well alone until the developer knows the difference between the stack and the heap

Westerberg
October 26th, 2006, 12:33 PM
Its true, I am working on the basics but I did find the out and ref parameter modifiers while searching. I'm using the code that wildfrog showed me since it works and a problem hasn't come up with it.

Skoons
October 26th, 2006, 02:49 PM
noooooo.. dont tell people that when they havent got the basics understood.. :)

out and ref parameter modifiers should be left well alone until the developer knows the difference between the stack and the heap
Why not, we always need to learn smth new :rolleyes:
And as for me the basics are heap, stack reference and other things that are principal to programming language. What was nice in C all of this was easy to understand during first applications and in c# you may not see them but often use