By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,188 Members | 1,610 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,188 IT Pros & Developers. It's quick & easy.

problem with System.Collecitons.Generic.List<>

P: n/a
Hello, there

I have a problem with regards to System.Collections.Generic.List<T>

I need to pass a class with implements an interface - TestClass :
IPerson
I put this class in a List<TestClass> = new List<TestClass>();

then I pass this List<TestClass> to a function which takes an argument
List<IPerson> person

It's giving me an error, that it cannot convert from List<TestClass> to
IPerson, how is this possible?

what I'm missing and doing wrong ? anyone knows what should I do to
pass that List<TestClass> ?

Apr 19 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
You need to create a List<IPerson> and add TestClass instances to it.
Then it will work, e.g.

List<IPerson> persons = new List<IPerson>();
persons.Add(new TestClass());

Josh
http://www.thejoyofcode.com/

Apr 19 '06 #2

P: n/a
If TestClass implements IPerson, why don't you just create a List<IPerson>,
put the TestClass instances into that, and pass that to the function?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"Varangian" <of****@gmail.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
Hello, there

I have a problem with regards to System.Collections.Generic.List<T>

I need to pass a class with implements an interface - TestClass :
IPerson
I put this class in a List<TestClass> = new List<TestClass>();

then I pass this List<TestClass> to a function which takes an argument
List<IPerson> person

It's giving me an error, that it cannot convert from List<TestClass> to
IPerson, how is this possible?

what I'm missing and doing wrong ? anyone knows what should I do to
pass that List<TestClass> ?

Apr 19 '06 #3

P: n/a
It's not that it can't convert the /contents/, but rather that it can't
convert the list itself. Just because A : B does not mean that List<A> :
List<B>.

The proper way to do this is to generalise the method; the parameter should
not be "a list of IPersons", but rather, "a list of /somethings/, which are
themselves IPersons"; you do this using the where operator:

public void SomeMethod<T>(List<T> items) where T : IPerson {
// do something
}

Note that this is type-safe; it knows that any item (e.g. items[0]) is a T,
which is itself an IPerson, so you get access to any members of IPerson;
however, .Add etc will only accept a T; because of this, if you plan to add
items to the collection, it can be useful to state "where T : IPerson,
new()"; this mandates that any T must have a public default ctor, hence you
can then (in SomeMethod) use "T newItem = new T();" to create new instances.

Hope this helps,

Marc

"Varangian" <of****@gmail.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
Hello, there

I have a problem with regards to System.Collections.Generic.List<T>

I need to pass a class with implements an interface - TestClass :
IPerson
I put this class in a List<TestClass> = new List<TestClass>();

then I pass this List<TestClass> to a function which takes an argument
List<IPerson> person

It's giving me an error, that it cannot convert from List<TestClass> to
IPerson, how is this possible?

what I'm missing and doing wrong ? anyone knows what should I do to
pass that List<TestClass> ?

Apr 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.