Click to See Complete Forum and Search --> : Collection of Members


ordonator
January 5th, 2010, 02:25 AM
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:


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?

nelo
January 5th, 2010, 03:40 AM
That doesn't look very difficult to me. Any other way would a little bit of complexity. Here's one solution

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...:)

ordonator
January 5th, 2010, 11:18 AM
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!