-
November 23rd, 2011, 10:31 AM
#1
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
-
November 23rd, 2011, 10:39 AM
#2
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.
Always use [code][/code] tags when posting code.
-
November 23rd, 2011, 10:38 PM
#3
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());
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
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
|