Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Collections

Collections

Scheduled Pinned Locked Moved C#
helpdata-structuresquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    haz13
    wrote on last edited by
    #1

    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

    L R 2 Replies Last reply
    0
    • H haz13

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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]

      H 1 Reply Last reply
      0
      • H haz13

        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

        R Offline
        R Offline
        RepliCrux
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • L Luc Pattyn

          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]

          H Offline
          H Offline
          haz13
          wrote on last edited by
          #4

          Thanks Luc for the useful info. I'm going to look into your suggestion about the IEnumerator. Such a steep learning curve only being new to c#:).

          Haz

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups