-
October 25th, 2006, 09:24 AM
#1
Return array from method
Is it possible to pass an array from a method?
An example below:
Code:
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
}
Last edited by Westerberg; October 25th, 2006 at 09:25 AM.
Reason: changed title
-
October 25th, 2006, 09:54 AM
#2
Re: Return array from method
Originally Posted by Westerberg
Is it possible to pass an array from a method?
Code:
yourType[] function()
{
// create array
yourType[] arr = ...;
// return array
return arr;
}
- petter
-
October 25th, 2006, 10:20 AM
#3
Re: Return array from method
-
October 25th, 2006, 06:03 PM
#4
Re: Return array from method
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
Code:
void SomeFunc(out type [] array)
{
//some actions with array
}
Last edited by Skoons; October 25th, 2006 at 06:06 PM.
God could improve essentially a
human nature, but he
was too anxious with compatibility
with the monkey.
(Eugeny Goldberg)
-
October 26th, 2006, 09:13 AM
#5
Re: Return array from method
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
-
October 26th, 2006, 11:33 AM
#6
Re: Return array from method
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.
-
October 26th, 2006, 01:49 PM
#7
Re: Return array from method
Originally Posted by cjard
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
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
God could improve essentially a
human nature, but he
was too anxious with compatibility
with the monkey.
(Eugeny Goldberg)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|