|
-
November 11th, 2008, 04:28 PM
#1
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;
}
-
November 11th, 2008, 04:38 PM
#2
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
-
November 11th, 2008, 04:47 PM
#3
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?
-
November 11th, 2008, 04:50 PM
#4
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
-
November 11th, 2008, 05:02 PM
#5
Re: Array problem
Ah, is there a way of going about having an effectively multiple dimensional List then, much like an array ?
-
November 11th, 2008, 05:07 PM
#6
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.
-
November 11th, 2008, 05:14 PM
#7
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?
-
November 11th, 2008, 05:25 PM
#8
Re: Array problem
So, I think that you actually wanted to use
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.
-
November 11th, 2008, 05:32 PM
#9
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;
}
-
November 11th, 2008, 05:36 PM
#10
Re: Array problem
Yup, that's where I was going. Create a sub List and then add it to the parent.
-
November 11th, 2008, 05:39 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|