CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Array problem

  1. #1
    Join Date
    Nov 2008
    Posts
    6

    Array problem

    Hello everyone,

    I'm trying to make an array from the return of a MySQL query but keep getting the following error message:

    Error 1 Use of unassigned local variable 'QueryArray'

    The line it refers to is the "return QueryArray;"

    I keep trying different methods to try and get this to work but I'm getting no where.. So any help would be much appreciated. It's a generic query handler I'm making, so I don't know how big the array is going to be before the query is executed..

    Code:
    public static string[][] ReturnQueryArray(MySql.Data.MySqlClient.MySqlConnection Source, string SQLQuery)
            {
                MySql.Data.MySqlClient.MySqlCommand MyQuery = Source.CreateCommand();
                MySql.Data.MySqlClient.MySqlDataReader Reader;
                MyQuery.CommandText = SQLQuery;
                Reader = MyQuery.ExecuteReader();
                
                int counter = 0;
                string[][] QueryArray;
    
                while (Reader.Read())
                {
                    for (int i = 0; i < Reader.FieldCount; i++)
                    {
                      QueryArray[counter][i] = Reader.GetValue(i).ToString(); 
                       
                    }
                    
                    counter = counter + 1;
                }
                return QueryArray;
            }

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Array problem

    Where are you ACTUALLY inisitalizing this:
    Code:
     string[][] QueryArray;
    hint: The compiler is correctly telling you that you have NOT!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Nov 2008
    Posts
    6

    Re: Array problem

    I thought that:

    Code:
    QueryArray[counter][i] = Reader.GetValue(i).ToString();
    Would count as initialising it?

    How can you initialise something prior to that when not knowing the length of either of the dimensions of the array?

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Array problem

    Arrays MUST be initialized by a direct or indirect call to new:

    Code:
    int [] a = new int[10];
    Arrays MUST have a known size at time of initialization. Collection Classes do not. See List<T>
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Nov 2008
    Posts
    6

    Re: Array problem

    Ah, is there a way of going about having an effectively multiple dimensional List then, much like an array ?

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Array problem

    Sure, you could create a list of lists.

    Code:
    List<List<string>> multi_list = new List<List<string>>();
    Last edited by BigEd781; February 17th, 2009 at 03:21 AM.

  7. #7
    Join Date
    Nov 2008
    Posts
    6

    Re: Array problem

    Ah, thanks for your help.. Sorry for all the questions, one last one, but how would you then Add to the list/list of lists?

    Wouldn't multi_lists.add() just add to the second list?

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Array problem

    So, I think that you actually wanted to use

    Code:
    string[ , ]
    instead of a jagged array. Using a List of Lists is actually pretty ugly, sorry. I am actually at work (!), so let me think about it. I would try to figure out the size that you need beforehand, but surely there are more experienced programmers around here that have a better answer for you.

  9. #9
    Join Date
    Nov 2008
    Posts
    6

    Re: Array problem

    Right, I've got it to work okay, here's what I did if you're curious..

    Code:
    public static List<List<string>> ReturnQueryArray(MySql.Data.MySqlClient.MySqlConnection Source, string SQLQuery)
            {
                MySql.Data.MySqlClient.MySqlCommand MyQuery = Source.CreateCommand();
                MySql.Data.MySqlClient.MySqlDataReader Reader;
                MyQuery.CommandText = SQLQuery;
                Reader = MyQuery.ExecuteReader();
                
                int counter = 0;
                List<List<string>> ResultList = new List<List<string>>();
                while (Reader.Read())
                {
                    List<string> ResultList2 = new List<string>();
                    for (int i = 0; i < Reader.FieldCount; i++)
                    {
                        ResultList2.Add(Reader.GetValue(i).ToString());
    
                     /* ResultList.Add(Reader.GetValue(i).ToString()); */
                       
                    }
                    ResultList.Add(ResultList2);
                    counter = counter + 1;
                }
                return ResultList;
            }

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Array problem

    Yup, that's where I was going. Create a sub List and then add it to the parent.

  11. #11
    Join Date
    Nov 2008
    Posts
    6

    Re: Array problem

    Yeah, there must be a better way but I guess this'll do for now.. Thanks for your help.

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