0

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();
}
1
  • You could initialize a collection of your object and add to it each time through the loop Commented Jan 28, 2014 at 21:08

8 Answers 8

2

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 });
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

0

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

Comments

0

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();

1 Comment

This code is wrong. You're converting to a Car[] not to a string[].
0

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

Comments

0

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

Comments

0

This should work:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.