CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Posts
    2

    Collection of Members

    Hey everyone. First time poster, long-time lurker. I may have found a question unanswered by search. Or I just don't know how to ask it.

    Any easy way to pass a collection of class members? Other than making a new list?

    So:

    Code:
    class Foo
    {
    public int x;
    
    }
    
    void MyFunc(List<int> list)
    {
        doSomething(list);
    }
    
    Main
    {
    List<Foo> listFoo = new List<Foo>();
    
    List<int> listInt = new List<int>();
    
    foreach (Foo foo in listFoo)
        listInt.Add(foo.x);
    
    MyFunc(listInt);
    
    }
    There's got to be something easier, right?
    Last edited by ordonator; January 6th, 2010 at 02:43 AM. Reason: Added code tags.

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Collection of Members

    That doesn't look very difficult to me. Any other way would a little bit of complexity. Here's one solution
    Code:
    interface IFoo
    {
       int X { get; set; }
    }
    
    class Foo : IFoo
    {
       int _x;
    
       public int X
       {
          get { return _x; }
          set { _x = value; }
       }
       
       // Add other properties that are specific to this class.
    }
    
    void MyFunc(List<IFoo> list)
    {
       
    }
    
    void Main()
    {
       List<IFoo> list = new List<IFoo>();
    
       MyFunc(list);
    }
    By the way you can use code tags to format your code. It makes it easier for others to read it, understand and then offer help. It also looks nicer...
    Last edited by nelo; January 5th, 2010 at 04:48 AM. Reason: Additional information...

  3. #3
    Join Date
    Jan 2010
    Posts
    2

    Re: Collection of Members

    I completely forgot about the code tag *facepalm*, and then I couldn't find the edit button later on. I was too tired to look for it.

    I was thinking I would have to use an interface. I was just hoping that System.Collections had yet another goodie I could learn about today. I think I'm all out of goodies though.

    Thanks for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured