CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2011
    Posts
    19

    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

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    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
  •  





Click Here to Expand Forum to Full Width

Featured