469,646 Members | 1,274 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

problem with System.Collecitons.Generic.List<>

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
3 1901
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
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
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.

Similar topics

14 posts views Thread by Dave | last post: by
3 posts views Thread by Eric | last post: by
2 posts views Thread by Brian Pelton | last post: by
4 posts views Thread by =?Utf-8?B?TGFycnlS?= | last post: by
7 posts views Thread by Andrew Robinson | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.