Click to See Complete Forum and Search --> : Array problem


mituozo
November 11th, 2008, 03:28 PM
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..


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;
}

TheCPUWizard
November 11th, 2008, 03:38 PM
Where are you ACTUALLY inisitalizing this:

string[][] QueryArray;


hint: The compiler is correctly telling you that you have NOT!

mituozo
November 11th, 2008, 03:47 PM
I thought that:


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?

TheCPUWizard
November 11th, 2008, 03:50 PM
Arrays MUST be initialized by a direct or indirect call to new:


int [] a = new int[10];


Arrays MUST have a known size at time of initialization. Collection Classes do not. See List<T>

mituozo
November 11th, 2008, 04:02 PM
Ah, is there a way of going about having an effectively multiple dimensional List then, much like an array ?

BigEd781
November 11th, 2008, 04:07 PM
Sure, you could create a list of lists.

List<List<string>> multi_list = new List<List<string>>();

mituozo
November 11th, 2008, 04:14 PM
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?

BigEd781
November 11th, 2008, 04:25 PM
So, I think that you actually wanted to use

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.

mituozo
November 11th, 2008, 04:32 PM
Right, I've got it to work okay, here's what I did if you're curious..


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;
}

BigEd781
November 11th, 2008, 04:36 PM
Yup, that's where I was going. Create a sub List and then add it to the parent.

mituozo
November 11th, 2008, 04:39 PM
Yeah, there must be a better way but I guess this'll do for now.. Thanks for your help.