user10425264
user10425264

Reputation:

C# Adding multiple items to list

Recently picked up C# from a python background. Currently experimenting with lists in C#. Lets say I designed an application that I wanted to store all the information about a car e.g. the name, make and colour. I also want it so that the user can add multiple cars to the list so they can be displayed later? How would I go about this task?

         private void Button_Click(object sender, RoutedEventArgs e)
    {
        List<string> Cars = new List <string>();

        string colour = txtInput.Text;

        Cars.Add(colour);

        lstDisplay.Items.Add(colour);

    }
}

}

Here is some code I have so far. The program takes input from the user and stores it, then adds it to the list and displays it to a list box. However I notice each time I add a new colour to the list it seems to always be empty? How would I make it so that the list will hold the input of different colours?

Upvotes: 3

Views: 9439

Answers (3)

abovetempo
abovetempo

Reputation: 150

To better answer your question I need a little more information. You can store the car information in memory but when the application is restarted all saved car information will be lost. If you need to keep track of the car information for an indefinite amount of time then you need to store the car information in something like a database.

class CarClass {

    private List<string> Cars;

    public CarClass() {
        Cars = new List <string>();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string colour = txtInput.Text;

        Cars.Add(colour);

        lstDisplay.Items.Add(colour);

    }

}

This would only store the Cars list in memory when the CarClass is instantiated and used. If you need it for later, when the CarClass is no longer referenced, you would need to save the carlist information in a database.

Upvotes: 1

Spixy
Spixy

Reputation: 313

If you have already have some cars when you are creating this list, you can do:

var list = new List<Car>(existingCars);

if not, then later do something like:

list.AddRange(existingCars);
list.Add(existingCar);

Edit: each time you are creating new list, so move this line

List<string> Cars = new List <string>();

from this method to class, like:

private readonly List<string> Cars = new List <string>();

Upvotes: 1

Timothy Winters
Timothy Winters

Reputation: 5821

If I'm reading you correctly, every time the button is clicked, you want to add a new color to the list, am I correct?

If that is the case, your problem is right here:

List<string> Cars = new List <string>();

You are recreating the list every time you press the button, essentially zeroing out the list. List<type> name = new List<type>(); creates a brand new list, even if it is called more than once. Therefore, when you call it in the method, you are doing just that.

Instead, you want to put that outside of the method, in the beginning of the class, so it is only created once. Something like:

class CarClass()
{
  List<string> Cars = new List <string>();

  private void Button_Click(object sender, RoutedEventArgs e)
  {
    string colour = txtInput.Text;
    Cars.Add(colour);
    lstDisplay.Items.Add(colour);
  }
}

This way, the list is created and initialized at the start of the class creation. Your method will then add to the list without zeroing it out.

Upvotes: 4

Related Questions