Jump to content

  • Log In with Google      Sign In   
  • Create Account


Need help creating new objects based on user input


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Manhattanisgr8   Members   -  Reputation: 486

Like
0Likes
Like

Posted 07 January 2014 - 09:25 PM

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;
        }
    }
}

Attached Thumbnails

  • Capture.JPG

Cpl Alt, Travis A

USMC


Sponsor:

#2 ChaosEngine   Crossbones+   -  Reputation: 2289

Like
2Likes
Like

Posted 07 January 2014 - 09:47 PM

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
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

#3 Manhattanisgr8   Members   -  Reputation: 486

Like
0Likes
Like

Posted 07 January 2014 - 10:26 PM


_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#.


Cpl Alt, Travis A

USMC





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS