473,405 Members | 2,287 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,405 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 2059
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.