Collections
-
I have a car class with various fields some of which are colour, make, mileage. I then have a car collection which contains multiple car objects.
public List<Car> CarCollection = new List<Car>();
What I want to do is pass the entire colour field array from the collection to a method CarData. (The method will be used to perform analysis on whatever array is passed in).CarData(CarCollection[].Colour);
I can't get it to work. Is this possible (I guessing not)? Do I need to create a copy somehow? Please suggest an efficient solution to my problem. Thanks in advance for any help.Haz
-
I have a car class with various fields some of which are colour, make, mileage. I then have a car collection which contains multiple car objects.
public List<Car> CarCollection = new List<Car>();
What I want to do is pass the entire colour field array from the collection to a method CarData. (The method will be used to perform analysis on whatever array is passed in).CarData(CarCollection[].Colour);
I can't get it to work. Is this possible (I guessing not)? Do I need to create a copy somehow? Please suggest an efficient solution to my problem. Thanks in advance for any help.Haz
Hi, there is no colour field array, you only have a lot of car objects, and some List containing all the references to them. So the way you could access them is:
foreach(Car car in CarCollection) { Color col=car.Colour; ... do something with col }
but doing so the field name is baked in the code. What you could do is: - make your analysis method accept an IEnumerator - give your CarCollection several new properties, each returning an IEnumerator for one of the Car fields. So now you could do: analyze(CarCollection.Colours); analyze(CarCollection.Make); with Colours returning an IEnumerator, etc. assuming Colour is a type (string or whatever) that represents the color. To actually implement the enumerator, I suggest you have a look at the yield keyword ! Not sure tho how much you can do in analyze(IEnumerator) if T can be anything; just check for duplicates, make histogram, ... ? :)Luc Pattyn [My Articles] [Forum Guidelines]
-
I have a car class with various fields some of which are colour, make, mileage. I then have a car collection which contains multiple car objects.
public List<Car> CarCollection = new List<Car>();
What I want to do is pass the entire colour field array from the collection to a method CarData. (The method will be used to perform analysis on whatever array is passed in).CarData(CarCollection[].Colour);
I can't get it to work. Is this possible (I guessing not)? Do I need to create a copy somehow? Please suggest an efficient solution to my problem. Thanks in advance for any help.Haz
I am assuming, the class car had array object color. When you try tp pass Array of color to the method CarData, because carCollection is a type of List, you need to past the index of the car collection which means: CarData(carCollection[0].Color); Cheers
-
Hi, there is no colour field array, you only have a lot of car objects, and some List containing all the references to them. So the way you could access them is:
foreach(Car car in CarCollection) { Color col=car.Colour; ... do something with col }
but doing so the field name is baked in the code. What you could do is: - make your analysis method accept an IEnumerator - give your CarCollection several new properties, each returning an IEnumerator for one of the Car fields. So now you could do: analyze(CarCollection.Colours); analyze(CarCollection.Make); with Colours returning an IEnumerator, etc. assuming Colour is a type (string or whatever) that represents the color. To actually implement the enumerator, I suggest you have a look at the yield keyword ! Not sure tho how much you can do in analyze(IEnumerator) if T can be anything; just check for duplicates, make histogram, ... ? :)Luc Pattyn [My Articles] [Forum Guidelines]