Mosfet
Mosfet

Reputation: 35

How to create object from class using a variable as name?

For example we have the next class :

class Car
{
    public string Make { get; set; }
    public string Model {get;set;}
    public int Year {get;set;}
    public string Color {get;set;}
}

And a array with a few car models :

string[] cars = { "Honda", "BMW", "Audi"};

I want to create an object for each item in the array. I've tried with the next code but without success

foreach (var item in cars)
{
    Car item = new Car();
}

Upvotes: 0

Views: 262

Answers (8)

Selman Genç
Selman Genç

Reputation: 101681

If you create your Car instance inside of foreach, you can't access these instances outside of foreach scope. First create a Car List :

var carList = new List<Car>();

Then use a for loop:

for(int i=0; i<cars.Length; i++)
{
   Car item = new Car();
   item.Make = cars[i];
   // set other properties
   carList.Add(item);
}

Upvotes: 1

rae1
rae1

Reputation: 6144

You cannot use the same variable item in the loop. Plus the instances aren't really used as they disappear once the loop ends.

You might want to use LINQ (a library used to manipulation collections) and map the string names over to a collection of Car objects,

var makes = { "Honda", "BMW", "Audi" };
var cars = makes.Select(make => new Car(make));

For this to work however you need to add a constructor to your class that takes a makers name,

public class Car
{
    public Car(string make)
    { 
        this.Make = make;
    }

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

Alternatively, you can use class property initializer to set the Make property,

var makes = { "Honda", "BMW", "Audi" };
var cars = makes.Select(make => new Car { Make = make });

Upvotes: 2

Allan Elder
Allan Elder

Reputation: 4094

You need to set some info on the car, and somewhere to store all the instances.

        string[] carMakes = { "Honda", "BMW", "Audi" };
        List<Car> cars = new List<Car>();

        foreach (var carMake in carMakes)
        {
            cars.Add(new Car(){Make = carMake});
        }

Upvotes: 1

pid
pid

Reputation: 11597

This should work:

foreach (string name in cars)
{
    Car item = new Car() { Make = name };
}

Upvotes: 0

user27414
user27414

Reputation:

You can construct each car like so:

foreach (var item in cars)
{
    // Note that you can't reuse 'item' here
    Car newCar = new Car { Make = item };
}

You will want to put each newCar in a list, or do something with it within the loop.

You could also use LINQ to do this in one line:

var carList = cars.Select(x => new Car { Make = x });

Upvotes: 0

Cam Bruce
Cam Bruce

Reputation: 5689

Add a constructor to your class that accepts a 'make' parameter. The set it to the Make member on your instance.

class Car
{
    public string Make { get; set; }
    public string Model {get;set;}
    public int Year {get;set;}
    public string Color {get;set;}

  public Car(string make)
  {
       this.Make = make;
  }
}

Upvotes: 0

D Stanley
D Stanley

Reputation: 152511

You can do this in one line with a little extra code

Car[] cars = new [] { new Car {Make = "Honda"}, 
                         new Car {Make = "BMW"}, 
                         new Car {Make = "Audi"}
                       };

or a Linq projection:

string[] cars = { "Honda", "BMW", "Audi"};
Car[] cars = cars.Select(s => new Car {Make = s})
                 .ToArray();

Upvotes: 0

mrzli
mrzli

Reputation: 17369

How about this:

class Car
{
    public string Make { get; set; }
    public string Model {get;set;}
    public int Year {get;set;}
    public string Color {get;set;}

    public Car(string make)
    {
        Make = make;
    }
}

And now for your loop:

List<Car> cars = new List<Car>();
foreach (var item in cars)
{
    Car car = new Car(item);
    cars.Add(car);
}

Upvotes: 0

Related Questions