• Advertisement
Sign in to follow this  

Need help creating new objects based on user input

This topic is 1473 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am playing around with windows forms and was wondering if there was a way to create a new object based on user input. Basically I want to be able to create a new object with what ever the user puts into the make textbox, add it to a List<Car>() and then display the model in the listbox. Just need to know if there is a way to make an object without knowing the name (such as Car xxx = new Car(make, model, year)) or knowing how many objects will be created and be able to pass in parameters. Below is the code for the simple car class and a screen capture of the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CreateNewObjectsTest
{
    class Car
    {
        private string make;
        private string model;
        private int year;

        public Car(string make, string model, int year)
        {
            this.make = make;
            this.model = model;
            this.year = year;
        }

        public string GetModel()
        {
            return model;
        }
    }
}

Share this post


Link to post
Share on other sites
Advertisement
Should be simple. Add an event handler to your create button and read the values of your text boxes.
public class Form1 : Form
{
  private readonly List<Car> _cars = new List<Car>();

  public Form1()
  {
    InitialiseComponent();

    btnCreate.Click += CreateCar;
  }

// ignore the params, they are required to match the event signature
  private void CreateCar(object sender, EventArgs args) 
  {
    _cars.Add(
      new Car(txtMake.Text, txtModel.Text, 
         int.Parse(txtYear.Text))); // need to convert year to integer

    // possibly some code to update the listbox, depending on how you populate it
  }
}
Also note that typically in C# you wouldn't have a GetModel() method, but more commonly a Model property (possibly readonly, depending on your design).
 
    class Car
    {
        public Car(string make, string model, int year)
        {
            Make = make;
            Model = model;
            Year = year;
        }

        public string Make {get; private set; }
        public string Model {get; private set; }
        public int Year {get; private set; }

    }
obviously that's very rough code and doesn't do any validation or error handling, but hopefully it should point you in the right direction

Share this post


Link to post
Share on other sites


_cars.Add( new Car(txtMake.Text, txtModel.Text, int.Parse(txtYear.Text))); // need to convert year to integer

 

Perfect, exactly what I was looking for. As far as populating the list box, all I did was override the ToString method to display the model of each car added.

 

Couldn't remember the easy way to use the getters and setters in C#. Decided to jump back into C# again after taking a programming class that uses java, just to transfer to a university that uses C++. Still enjoy coding in C#.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement