Annie Lowry Annie Lowry - 2 months ago 12
C# Question

Displaying Multiple Arrays

I have a project where I have to take in input of names, weights, and heights and put them into arrays and display them into a textbox like this

name = "..."
weight = "..."
height= "..."


I have been able to populate my arrays but I do not understand how to it output it like the example above. Currently my output is all names, THEN all weights then all heights. Could someone explain how I would be able to make it display like the example? The code I have so far is

private void ShowButton_Click(object sender, EventArgs e)
{

txtShow.Text += string.Join(System.Environment.NewLine, nameArray);
txtShow.Text += string.Join(System.Environment.NewLine, weightArray);
txtShow.Text += string.Join(System.Environment.NewLine, heightArray);


}
private void AddButton_Click(object sender, EventArgs e)
{
if (this.index < 10)
{
nameArray[this.index] = nameBox.Text;
weightArray[this.index] = double.Parse(weightBox.Text);
heightArray[this.index] = double.Parse(heightBox.Text);

this.index++;
}
}


The array can store up to 10 values and I am required to use arrays and not lists. Thank you!

Answer
 private void ShowButton_Click(object sender, EventArgs e)
 {      string myString = "";
        for(int i=0;i<nameArray.Length;i++)
        {
              myString+= "Name: "+nameArray[i]+", ";
              myString += "Weight: "+weightArray[i]+", ";
              myString += "Height: "+heightArray[i]+"\n");
        }
        txtShow.Text = myString;
 }

or, create a class of person with name, height, weight and create a list of people, then join the list of people.

PS - on first answer, I tried to keep my suggestion code as close to yours as possible - to answer your question without changing your code. Another commenter suggested that contantly updating a textbox.Text will lead to blinking issues. If that happens to you, I'd suggest using a temporary string as I've edited my text to do.

I know this is homework - so I'm not suggesting any grand optimizations that will make you look like you've been getting your homework done at SO.

Comments