Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 380
Annie Lowry Avatar asked Sep 15 '16 12:09

Annie Lowry


2 Answers

You Should::::: Lots of ways to optimize what you're trying to do, but this is homework and you don't want to look like you're the world's greatest programmer -- you want to do the project like the prof is expecting you to. So creating classes and joining lists is not part of your particular solution set. Try:

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.

EDIT You've asked for a way to detect empty. Based on my understanding of your code and keeping it simple, try:

 private void AddButton_Click(object sender, EventArgs e)
{
    if (this.index < 10)
    {
        if(nameBox.Text.Length==0||weightBox.Text.Length==0||heightBox.Text.Length==0){
           MessageBox.Show("You must enter a name, weight, and height!");
        }else{
            nameArray[this.index] = nameBox.Text;
            weightArray[this.index] = double.Parse(weightBox.Text);
            heightArray[this.index] = double.Parse(heightBox.Text);

            this.index++;
            nameBox.Text = "";
            weightBox.Text = "";
            heightBox.Text = "";
         }   
     }
}
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;

 }

NOTE Textboxes have validation methods which will do the work of my IF/THEN statement in my revised edit to find empties. If you think the prof is looking for form (control) validation instead of codebehind IF/THEN, let me know and I'll help with that.

Okay - you mentioned the need to sort. To do that, we need to use some way of grouping the input data. We could use Dictionary or class. Let's go with class:

Putting it all together: Have a look at this potential solution - if you think it's too complicated for what your homework should look like, we can try to simplify. Let me know:

public class Person{
    public string Name {get;set;}
    public double Height {get;set;}
    public double Weight {get; set;}
    public string Print(){
         return "Name: "+Name+", Height: "+Height.ToString()+", Weight: "+Weight.ToString()+"\r\n";
    }
}
Person[] People = new Person[10];
int thisIndex = 0;
private void AddButton_Click(object sender, EventArgs e)
    {
        if (this.index < 10)
    {
        if(nameBox.Text.Length==0||weightBox.Text.Length==0||heightBox.Text.Length==0)
        {
             MessageBox.Show("You must enter a name, weight, and height!");
        }else{
            Person p = new Person();
            p.Name = nameBox.Text;
            p.Weight = double.Parse(weightBox.Text);
            p.Height = double.Parse(heightBox.Text);
            People[thisIndex] = p;
            thisIndex++;
            nameBox.Text = "";
            weightBox.Text = "";
            heightBox.Text = "";
         }   
     }
}
private void ShowButton_Click(object sender, EventArgs e)
 {      
       People = People.OrderBy(p=>p.Name).ToArray();
        string myString = "";
        for(int i=0;i<10;i++)
        {
            if(People[I]!=null){
              myString+= People[I].Print();
            }
        }
        txtShow.Text = myString;

 }
like image 180
Shannon Holsinger Avatar answered Oct 13 '22 21:10

Shannon Holsinger


You should create a class for this purpose. A class enables you to create your own custom types by grouping together variables of three types (string,double and double):

public class Person
{
    public string Name { get; set; }
    public double Weight { get; set; }
    public double Height { get; set; }

    public override string ToString()
    {
        return Name + " " + Weight + " " + Height;
    }
}

Then:

Person[] persons = new Person[10];
private void AddButton_Click(object sender, EventArgs e)
{
     persons[index] = new Person
        {
            Name = nameBox.Text,
            Weight = double.Parse(weightBox.Text),
            Height = double.Parse(heightBox.Text)
        };
        index++;
}

And finally (look at the ShowButton's code that now is one line):

txtShow.Text += string.Join(System.Environment.NewLine, persons.AsEnumerable());
like image 3
Salah Akbari Avatar answered Oct 13 '22 23:10

Salah Akbari