CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2006
    Posts
    12

    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

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Return array from method

    Quote 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

  3. #3
    Join Date
    Oct 2006
    Posts
    12

    Re: Return array from method

    Thanks.

  4. #4
    Join Date
    Mar 2004
    Location
    Ukraine
    Posts
    170

    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)

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    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
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  6. #6
    Join Date
    Oct 2006
    Posts
    12

    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.

  7. #7
    Join Date
    Mar 2004
    Location
    Ukraine
    Posts
    170

    Re: Return array from method

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured