Name
Name

Reputation: 529

C# create a new object of a class after pressing a button

I never programmed object-oriented, but now I have the breakthrough :D

I created an easy Windows Form in Visual Studio with 1 Button. If I press this button I want to create a object of the class car. = if I press the button 20x I created 20 object's of the class car.

I know how to create one class manually like this:

Car myCar = new Car();

How can I give every object a different name? Or is this the wrong way and I create a array object(? I dont know if a array object exists).

Upvotes: 1

Views: 4021

Answers (4)

Whit Waldo
Whit Waldo

Reputation: 5207

This isn't an OOO-specific question, but as it pertains to your question in C#...

Well, when you're creating a new object, unless you're storing it somewhere, it'll be disposed whenever the garbage collector runs. As Steve mentioned in the comment, if you're storing 20 instances of the Car, you might want to store them in an object that inherits from the IEnumerable generic object like a IList in which case you don't necessarily reference them by name, but rather just by the index in which they're located within the IList:

IList<Car> list = new List<Car>();
for(var a = 0; a < 5; a++) {
    list.Add(new Car());
}

//To access the second Car instance, remember that we have zero-based indices in the C# world, so you'd use...
Car instance = list[1];

This would yield that second instance without necessarily needing a name or other unique identifier. But lets say that you do have some sort of unique identifier for each of your Car objects that's created when you instance the object such as in the following constructor:

class Car {
    public Car()
    {
        UniqueIdentifier = new Guid().ToString();
    }

    public string UniqueIdentifier { get; private set; }
}

Whenever you create a new instance of Car, this will automatically assign a GUID, or value that can (for the purposes here) be assumed to be unique when the Car is created. Then, in the future, assuming you know these GUID values, you could use a LINQ statement to pull your Car instance from the IList according to that specific GUID value:

Car specificCar = list.FirstOrDefault(list, a => a.UniqueIdentifier == "<YOUR SPECIFIC GUID VALUE>");

So, you have some options you could utilize here for how you'd like to access your newly created objects.

Upvotes: 4

Lukas K&#246;rfer
Lukas K&#246;rfer

Reputation: 14493

I think all the discussions about Arrays and Lists do not cover the questioners problems with OOP, because these containers are only for storing objects:

How can I give every object a different name?

The variables name should not be the name of the represented object (in your case, a car). If you think the "real" object behind your object created in code should have a name or its kind of reasoned to give it one, add a property with the name "Name" to its description (the class) and assign each name to another new object. The "art" of variable naming is tricky and there are a lot of different opinions about it.

So think about creating multiple "cars" by doing:

Car car1 = new Car("Audi A4");
Car car2 = new Car("Ford Fiesta");

instead of:

Car audiA4 = new Car();
Car fordFiesta = new Car();

Edit:

Of course, my first example needs a car class constructor assigning the name to an underlying property.

Upvotes: 1

sara
sara

Reputation: 3589

One way to solve this is to store a collection of Car objects in the Form class and then add a new one when you press the button.

public class MyForm : Form
{
    private IList<Car> _cars;

    ...

    public void myButton_OnClick(object sender, EventArgs e)
    {
        _cars.Add(new Car());
    }

    ...

}

You can then access your cars by iterating over the _cars list, or by using index accessing.

EDIT: As was pointed out in a comment, this isn't really object oriented programming though. OOP is a pretty complex subject, a way to model and solve problems. C# and classes can be used to this end, but code written in C# isn't necessarily object oriented. More info can be found on Wikipedia/in various books.

Upvotes: 2

Fab
Fab

Reputation: 139

You can create an array of objects, or maybe a list, like this:

// In the class definition
private List<Car> MyObjectList = new List<Car>();

//In the event handler
MyObjectList.Add(new Car());

Upvotes: 1

Related Questions