Mr.AwfulAtProgramming Mr.AwfulAtProgramming - 2 months ago 25
C# Question

ArrayList to MessageBox C#

I was just wondering how can I print an entire ArrayList in a MessageBox?
Here is what I currently have:

ArrayList myData = new ArrayList();
...bunch of Strings added to myData...
private void btnDisplayScores_Click(object sender, EventArgs e)
{
MessageBox.Show(myData); <-----what I need help with
}

Answer

You can try converting an obsolete ArrayList to IEnumerable<Object> and then Join items together:

 MessageBox.Show(string.Join(Environment.NewLine, myData.OfType<Object>()));

A better design is to change ArrayList to List<Object> (or List<String> if myData should have just String items):

 List<Object> myData = new List<Object>();
 ...
 MessageBox.Show(string.Join(Environment.NewLine, myData));
Comments