PC Review


Reply
Thread Tools Rate Thread

Convert from List<A> to List<B>

 
 
bob_jeffcoat@hotmail.com
Guest
Posts: n/a
 
      20th Jun 2006
Hi,

Have class B which subclasses from class A. I have a List<B> and I'm
trying to get a List<A> without writing a stupid loop everytime I want
to do it. So I tried this method:

public static List<Destination> ConvertList<Source,
Destination>(List<Source> sourceList) where Destination : Source
{
List<Destination> result = new List<Destination>();
foreach (Source aSource in sourceList)
{
result.Add(aSource as Destination);
}
return result;
}

It didn't work, is it possible?

Why isn't there a method like this:
List<B> aList = new List<B>();
List<A> newList = aList.Convert<A>();

Any ideas?

Thanks,

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      20th Jun 2006
Like so?

List<B> aList = new List<B>();
List<A> newList = aList.ConvertAll<A>(delegate(B item) {return (A) (object)
item;});

The (object) cast is needed where an explicit cast operator is not provided
by the class - or you could build the replacement objects yourself.

Marc


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      20th Jun 2006
Additional: I didn't spot the "B subclasses A"; this means you can drop the
(object) cast

Finally - is this so you can can pass a List<A> as method parameters? If so,
note that this is a covariance issue, and can be resolved through generics
(see Right() below)

Marc

static void Main() {
List<B> aList = new List<B>();
List<A> newList = aList.ConvertAll<A>(delegate(B item) { return
(A)item; });

Wrong(aList);
Right(aList);
}
public static void Wrong(List<A> items) {
//do stuff with each A
}
public static void Right<T>(List<T> items) where T : A{
//do stuff with each T, which can also be treated as A
}
public class A { }
public class B : A { }


 
Reply With Quote
 
james.curran@gmail.com
Guest
Posts: n/a
 
      20th Jun 2006
That looks OK. How was it failing?

(E-Mail Removed) wrote:
> Have class B which subclasses from class A. I have a List<B> and I'm
> trying to get a List<A> without writing a stupid loop everytime I want
> to do it. So I tried this method:
> [snip]
> It didn't work, is it possible?
>


 
Reply With Quote
 
Mark Wilden
Guest
Posts: n/a
 
      20th Jun 2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> Have class B which subclasses from class A. I have a List<B> and I'm
> trying to get a List<A> without writing a stupid loop everytime I want
> to do it. So I tried this method:
>
> public static List<Destination> ConvertList<Source,
> Destination>(List<Source> sourceList) where Destination : Source
> {
> List<Destination> result = new List<Destination>();
> foreach (Source aSource in sourceList)
> {
> result.Add(aSource as Destination);
> }
> return result;
> }
>
> It didn't work, is it possible?


If you reverse the where constraint, this works:

public static List<Destination> ConvertList<Source,
Destination>(List<Source> sourceList)
where Source : Destination
{
List<Destination> result = new List<Destination>();

foreach (Source aSource in sourceList)
result.Add(aSource);

return result;
}

///ark


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert a generic list to Outlook.Items list? jeremybo Microsoft Outlook Program Addins 0 2nd Apr 2009 02:29 PM
Convert Array to Generic List and find object in list Chris Kennedy Microsoft Dot NET Framework 1 31st Aug 2008 08:04 PM
How to convert vertical blocked list to horizontal list? G Lykos Microsoft Excel Worksheet Functions 4 18th Dec 2006 02:06 PM
how do I convert my contacts list to an e-mail address list =?Utf-8?B?bXJiaWxs?= Microsoft Word Document Management 1 22nd Mar 2006 04:28 AM
Convert List box from excel file to VBA list box object baha Microsoft Excel Programming 0 22nd Nov 2003 05:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:45 PM.