Reputation: 2073
hopefully a pretty simple question this time. I have a Select method in a database connector class, which is as follows;
public List <string> [] Select(string mQuery)
{
//Create a list to store the result
List<string>[] datalist = new List<string>[1];
datalist[0] = new List<string>();
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(mQuery, mConnection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
datalist[0].Add(dataReader["id"] + "");
}
//close Data Reader
dataReader.Close();
//close Connection
this.CloseConnection();
//return list to be displayed
return datalist;
}
else
{
return datalist;
}
}
When i want to access "datalist" i'm assuming i call it like so;
results = mDB.Select(mQuery);
But because the returned value is a list, do i need to assign this variable to a new list, like so;?
List<string>[] results = new List<string>[1];
results[0] = new List<string>();
results = mDB.Select(mQuery);
string result = results[0].ToString();
MessageBox.Show(result);
This message box simply produces "System.Collections.Generic.List1(System.String)"
Any ideas about the logic of what i'm doing wrong?
Upvotes: 0
Views: 1894
Reputation: 20616
If what you're trying to do is display the contents of a list in e.g. a comma-separated way, you can do something like this:
MessageBox.Show(string.Join(",", list));
The reason you're getting "System.Collections.Generic.List1(System.String)" is that ToString
for a List
just returns a string representation of its type.
Also, as others have pointed out, you should lose the array. What you're after is something like:
public List<string> Select(string mQuery)
{
//...
}
List<string> list = mDB.Select(mQuery);
MessageBox.Show(string.Join(",", list));
Upvotes: 0
Reputation: 2476
Try not wrapping your list in an array?
List<string> results = new List<string>();
results = mDB.Select(mQuery);
string result = results[0].ToString();
MessageBox.Show(result);
Either way, the issue is that you were trying to display a list, which by default just returns it's type. You should display the members of the list, not the list itself.
Upvotes: 2