Thread: Get a list of member variables

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    26

    Accessing member variables of a list

    Hey everyone, I've been wondering about this, but can't find anything about it.

    Basically, I've got a class, and a list of that class:

    Code:
    public class Person
    {
        public Rectangle boundingBox;
        public int health;
    }
    
    List<Person> personList = new List<Person>();
    And I've got a function that takes a list of rectangles:

    Code:
    private void CheckBoundingBoxes(List<Rectangle> boundingBoxes)
    {
        // Do stuff...
    }
    Now, is it possible that instead of doing this to generate a list of rectangles based on each persons bounding box:

    Code:
    List<Rectangle> boundingBoxList = new List<Rectangle>(personList.Count);
    
    for (int i = 0; i < personList.Count; ++i)
    {
        boundingBoxList.Add(personList[i].boundingBox);
    }
    
    CheckBoundingBoxes(boundingBoxList);
    I can just access the bounding box variable directly, something like:

    Code:
    CheckBoundingBoxes(new List<Rectangle>( personList { boundingBox } ) );
    to do the same thing?

    I appreciate any help I get in regards to this
    Last edited by HLMetroid; 07-06-2009 at 07:30 PM.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You could probably use a foreach() loop to ease it a bit:
    Code:
    List<Rectangle> boundingBoxList = new List<Rectangle>();
    
    foreach(Person p in personList)
        boundingBoxList.Add(p.boundingBox);
    
    CheckBoundingBoxes(boundingBoxList);
    LINQ might be able to do something like this as well, I believe what would do it is:
    Code:
    from p in personList select p.boundingBox
    ...but I have no idea what type that returns. :-) Regardless, it's probably an IEnumerable<Rectangle>, so you might want to consider having your CheckBoundingBoxes function accept that instead of a list.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Thanks for that, I'll try it out

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Use:
    Code:
    using System.Linq;
    
    CheckBoundingBoxes(personList.Select(p => p.boundingBox).ToList());
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Thanks, that seems like a short way of doing it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Conflicting types of typedef error
    By advocation in forum C++ Programming
    Replies: 4
    Last Post: 03-22-2005, 06:26 PM
  5. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM