-
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;
}
-
Re: Array problem
Where are you ACTUALLY inisitalizing this:
Code:
string[][] QueryArray;
hint: The compiler is correctly telling you that you have NOT!
-
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?
-
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>
-
Re: Array problem
Ah, is there a way of going about having an effectively multiple dimensional List then, much like an array ?
-
Re: Array problem
Sure, you could create a list of lists.
Code:
List<List<string>> multi_list = new List<List<string>>();
-
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?
-
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.
-
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;
}
-
Re: Array problem
Yup, that's where I was going. Create a sub List and then add it to the parent.
-
Re: Array problem
Yeah, there must be a better way but I guess this'll do for now.. Thanks for your help.