Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have created a Class called "Car" and then I created another Class called "CarSourceItem". Through the Console program I am unable to print the Car type objects via Foreach loop by creating object of the CarSourceItem.

Car.cs
class Car
    {
        public string Brand { get; set; }
        public int Wheels { get; set; }
        public string Paint { get; set; }
        public string GearBox { get; set; }
    }


CarSourceItem.cs
C#
class CarItemSource
    {
        public List<Car> cars { get; set; }

        public CarItemSource()
        {
            new Car { Brand = "Audi", GearBox = "Automatic", Paint = "Yello", Wheels = 4 };
            new Car { Brand = "Maruti 800", GearBox = "Manual", Paint = "White", Wheels = 4 };
            new Car { Brand = "Vaux", GearBox = "Manual", Paint = "Dark Black", Wheels = 4 };
            new Car { Brand = "TATA", GearBox = "Manual", Paint = "Lime Green", Wheels = 4 };
         }

    }



Help me please.
Posted
Comments
Gopi Kishan Mariyala 14-Jan-14 23:42pm    
I have a question. Why both classes Car and CarItemSource are not declared public. Is it intentional?

Gopi Kishan Mariyala put you on the right track here: the essential problem here is that the Cars collection is not initialized.

You can save some typing like this:
C#
public class Car
{
    public string Brand { get; set; }
    public string GearBox { get; set; }
    public string Paint { get; set; }
    public int Wheels { get; set; }

    public Car(string brand, string gearbox, string paint, int wheels)
    {
        Brand = brand;
        GearBox = gearbox;
        Paint = paint;
        Wheels = wheels;
    }
}

public class CarItemSource
{
    public List<Car> cars { get; set; }

    public CarItemSource()
    {
        cars = new List<Car> 
        {
            new Car ("Audi","Automatic","Yello", 4 ),
            new Car ("Maruti 800", "Manual", "White", 4 ),
            new Car ("Vaux", "Manual", "Dark Black", 4 ),
            new Car ("TATA", "Manual", "Lime Green", 4 )
        };
    }
}

It is tedious to pass in a bunch of parameters and have to assign them to the Class' variables: but, a good reason to get in the habit of doing this is that the next version of C# may automatically generate the Property variable back-stores for you: [^].

However, note that in the above, and in all the responses to this thread, that you could create any number of new instances of the CarItemSource Class, and each one will repeat having the identical internal List<Car>. imho, that is not a good thing.

So, I suggest you reconsider your design here, with the goal of creating only one unique List<Car>.

You might think you could solve this using this type of Class definition:
C#
public class CarItemSource: List<Car>
{
    public CarItemSource()
    {
        if(this.Count == 0)
        {
            this.AddRange( new List<Car>
            {
                new Car ("Audi","Automatic","Yello", 4 ),
                new Car ("Maruti 800", "Manual", "White", 4 ),
                new Car ("Vaux", "Manual", "Dark Black", 4 ),
                new Car ("TATA", "Manual", "Lime Green", 4 )
            });
        }
    }
}
But, that has the same problem: you can create multiple instances of CarItemSource, and the List<Car> is re-created (I have had more than one student who thought they solved this type of problem with this type of clever strategy !).

The obvious alternative is making CarItemSource a static Class:
C#
public static public class CarItemSource
{
    public static List<Car> CarList = new List<Car>
    {
        new Car ("Audi","Automatic","Yello", 4 ),
        new Car ("Maruti 800", "Manual", "White", 4 ),
        new Car ("Vaux", "Manual", "Dark Black", 4 ),
        new Car ("TATA", "Manual", "Lime Green", 4 )
    };
}
Test, somewhere in a method, like this:
Car aCar = CarItemSource.CarList[0];

string carName = aCar.Brand;
Classes that have one-and-only-one possible instance are, technically, referred to as "Singletons." For an in-depth consideration of Singletons in C#, Jon Skeet, as usual, has the definitive analysis: [^].
 
Share this answer
 
v5
The CarItemSource should be modified as below. The list object was not created. And after creating list object, we have to add car items.

XML
public List<Car> cars { get; set; }

        public CarItemSource()
        {
            cars = new List<Car>();
            cars.Add(new Car { Brand = "Audi", GearBox = "Automatic", Paint = "Yello", Wheels = 4 });
            cars.Add(new Car { Brand = "Maruti 800", GearBox = "Manual", Paint = "White", Wheels = 4 });
            cars.Add(new Car { Brand = "Vaux", GearBox = "Manual", Paint = "Dark Black", Wheels = 4 });
            cars.Add(new Car { Brand = "TATA", GearBox = "Manual", Paint = "Lime Green", Wheels = 4 });
         }
 
Share this answer
 
v2
Comments
BillWoodruff 15-Jan-14 9:31am    
+4 You are on the right track here: the key problem in the OP is that the List of Car was never initialized, and the new instances of 'Car never added to the List.
Try this


C#
public CarItemSource()
    {
        cars.Add(new Car { Brand = "Audi", GearBox = "Automatic", Paint = "Yello", Wheels = 4 });
        cars.Add(new Car { Brand = "Maruti 800", GearBox = "Manual", Paint = "White", Wheels = 4 });
        cars.Add(new Car { Brand = "Vaux", GearBox = "Manual", Paint = "Dark Black", Wheels = 4 });
        cars.Add(new Car { Brand = "TATA", GearBox = "Manual", Paint = "Lime Green", Wheels = 4 });
    }
 
Share this answer
 
Create a property for cal list and then get that property in console application:
C#
using System;
using System.Collections.Generic;

namespace ConsoleApplicationExample
{
    class Program
    {
        static void Main(string[] args)
        {
            CarItemSource carItems = new CarItemSource();
            foreach (Car car in carItems.Cars)
            {
                Console.WriteLine("Barnd :{0}, Wheels :{1},Paint:{2},GearBox:{3}", car.Brand,car.Wheels,car.Paint,car.GearBox);
            }
            Console.ReadKey();
        }
    }

  
    
        class Car
        {
            public string Brand { get; set; }
            public int Wheels { get; set; }
            public string Paint { get; set; }
            public string GearBox { get; set; }
        }


        class CarItemSource
        {
            public IList<Car> Cars { get { return new List<Car>()
            {
                new Car { Brand = "Audi", GearBox = "Automatic", Paint = "Yello", Wheels = 4 },
                 new Car { Brand = "Maruti 800", GearBox = "Manual", Paint = "White", Wheels = 4 },
                 new Car { Brand = "Vaux", GearBox = "Manual", Paint = "Dark Black", Wheels = 4 },
                 new Car { Brand = "TATA", GearBox = "Manual", Paint = "Lime Green", Wheels = 4 }
            };
            }
            }

            public CarItemSource()
            {
                
            }

        }

}
 
Share this answer
 
v3
Comments
BillWoodruff 15-Jan-14 9:36am    
This will not compile. It's easy enough to fix, though. I suggest you verify your code compiles before posting it.
Sandeep Singh Shekhawat 15-Jan-14 10:46am    
Thanks dear BillWoodruff. It was my formatting mistake whenever I paste it from VS2010. I had compiled and run on my local development machine before post it. When I post it then “Car” class list converted into “car” list due to best guess formatting and I didn’t notice it because I am new here. I haven’t intension to misguide anybody here. Thanks to you to notify me and I got punished by devoted it. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900