problems with arraylist and messagebox
hello there, i want to show all the elements of an arraylist into a single messagebox.im at a loss about what to do.because messageboxes only take strings as parameters, how can i concatenate an arraylist into a single string?and i DO NOT want multiple messageboxes.i can think of no better than this,where it only shows the last value of the arraylist-----------
Code:
string hehe = "";
for (int i = 0; i < namesList.Count; i++)
{
hehe = "" + namesList[i];
}
MessageBox.Show(hehe);
please help me.thanks in advance
lamiajoyee
Re: problems with arraylist and messagebox
You are replacing the content of your string with each pass so you get only the last entry. To get them all you need to keep adding to the string.
Code:
hehe += "" + namesList[i];
You may also want to append a CRLF to the end so each item is on a seperate line.
Re: problems with arraylist and messagebox
For best performance use StringBuilder in the System.Text namespace to build up a string instead of repeatedly concatenating strings :
Code:
System.Text.StringBuilder hehe = new System.Text.StringBuilder();
for (int i = 0; i < namesList.Count; i++)
{
hehe.AppendLine(namesList[i]);
//Or just .Append() if you don't want to append a newline per DataMiser's suggestion
}
MessageBox.Show(hehe.ToString());