5

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.

2

3 Answers 3

4

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;

 }
Sign up to request clarification or add additional context in comments.

12 Comments

Thank you! This made the order correct but the newline isn't working :(. Edit: Nevermind \r\n worked! Thank you!
Glad to help! Cheers.
By chance do you know how to make it ignore empty or null values? I know there is a method isEmptyOrNull() but I don't know how to incorporate it here.
I think the revised answer should help (see my edit). You may want to add buttons to your messagebox, but you'll get the general idea...
I just edited again - I got to thinking that your prof may want to see form validation for the textboxes as opposed to codebehind. If you are learning about form validation, then let me know and we'll go in that direction instead of the IF/THEN statement I posted earlier.
|
3

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());

3 Comments

Thank you for the reply but isn't this using array list? When I get to use arraylists I will try this :).
@AnnieLowry I used List instead of array because it is more generic way. If you still want to use array shouldn't it be too hard just change the list definition to array.
@AnnieLowry Check my updated answer. It use array instead of list now.
3

Linq solution:

private void ShowButton_Click(object sender, EventArgs e) {
  int n = Math.Min(Math.Min(nameArray.Length, weightArray.Length), heightArray.Length));

  txtShow.Text = String.Join(Environment.NewLine, Enumerable
    .Range(0, n)
    .Select(i => string.Format("Name: {0}, Weight: {1}, Height: {2}",
       nameArray[i], weightArray[i], heightArray[i]))); 
}

Try avoiding txtShow.Text += fragments, especially in loops (each Textchanging means redrawing and thus blinking)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.