-2

enter image description here

class CarMaker
{
    public void MakeNewCar(int carCount)
    {
        string[] names = new string[carCount];
        Random random = new Random();            
        for (int i = 0; i < carCount; i++)
        {
            int randomNum = random.Next(0, 3);
            Car **howCanIDo** = new Car(null, (EModels)randomNum, (EColor)randomNum);
        }
    }
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95

1 Answers1

3

By the collection names I assume you want to "hold" all the "names" (actually the references) to the Car objects you create in the loop. What you could do is instead have an array of type Car and add the items to it:

Car[] cars = new Car[carCount];
Random random = new Random();
for (int i = 0; i < carCount; i++)
{
    int randomNum = random.Next(0, 3);
    cars[i] = new Car(null, (EModels)randomNum, (EColor)randomNum);
}

And maybe even more convenient would be to just use lists:

List<Car> cars = new List<Car>();
for (int i = 0; i < carCount; i++)
{
    int randomNum = random.Next(0, 3);
    cars.Add(new Car(null, (EModels)randomNum, (EColor)randomNum));
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95