473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2058
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
3
by: Eric | last post by:
I have a string representation of an object. I create an object of that type through reflection. I would like to create a List<> of those objects. I obviously can't do List<myObject.GetType()>...
2
by: Brian Pelton | last post by:
I am not sure how to fix this problem I've stumbled into... I have a list<> of an interface type. I need to pass that list to a method that adds more objects to the list. But, eventually, I...
6
by: Jeff.Boeker | last post by:
I'm learning a lesson in how I need to be more specific :) In C++ I can resize a vector and it will allocate memory and it will call the default constructor if necessary (or I can supply an...
4
by: =?Utf-8?B?TGFycnlS?= | last post by:
I need some help with a multilevel sorting problem with the List<>. I have a List<ItemToSort( see below ) that needs to be sorted in the following manner: Sort by Level1Id ( ok that was the easy...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
56
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
4
by: Peted | last post by:
I have the following code public enum pdfFlags { PFD_DRAW_TO_WINDOW, PFD_DRAW_TO_BITMAP, PFD_SUPPORT_GDI, PFD_SUPPORT_OPENGL, PFD_GENERIC_ACCELERATED, PFD_GENERIC_FORMAT,
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.