473,656 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic collection casting

Hi,

I would like to have something like that :

IList<IUser> list2 = list1 as IList<IUser>;

where list1 is a generic collection of User ( IList list1<User>=new
List<User>() ) and where User implement the interface IUser.
But this did not work...
I thought that as User implement IUser it was possible to cast the generic
collection between the two types. Currently I ve to use a foreach loop to
populate the list2 collection :
foreach(User u in list1)
{
list2.add(u as IUser);
}

Is there a more efficient and elegant way for doing this ?

Thanks.
Yalt.


Apr 14 '06 #1
2 1718
The problem here is that while the User implements the IUser interface, and
therefore an instance of User can be cast as IUser, IList<IUser> is a *List*
of IUser, and IList<User> is a *List* of User. You're not trying to cast the
items *u=in* the list; you're trying to cast the List itself. List<User>
does not inherit List<IUser> and List<IUser> is not an interface which
List<User> Implements. Therefore, there is no casting between them.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

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

"cylt" <cy**@newsgroup s.nospam> wrote in message
news:E9******** *************** ***********@mic rosoft.com...
Hi,

I would like to have something like that :

IList<IUser> list2 = list1 as IList<IUser>;

where list1 is a generic collection of User ( IList list1<User>=new
List<User>() ) and where User implement the interface IUser.
But this did not work...
I thought that as User implement IUser it was possible to cast the generic
collection between the two types. Currently I ve to use a foreach loop to
populate the list2 collection :
foreach(User u in list1)
{
list2.add(u as IUser);
}

Is there a more efficient and elegant way for doing this ?

Thanks.
Yalt.

Apr 14 '06 #2
Thank you for your response,

I understand now what was wrong.

Yalt.
"Kevin Spencer" wrote:
The problem here is that while the User implements the IUser interface, and
therefore an instance of User can be cast as IUser, IList<IUser> is a *List*
of IUser, and IList<User> is a *List* of User. You're not trying to cast the
items *u=in* the list; you're trying to cast the List itself. List<User>
does not inherit List<IUser> and List<IUser> is not an interface which
List<User> Implements. Therefore, there is no casting between them.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

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

"cylt" <cy**@newsgroup s.nospam> wrote in message
news:E9******** *************** ***********@mic rosoft.com...
Hi,

I would like to have something like that :

IList<IUser> list2 = list1 as IList<IUser>;

where list1 is a generic collection of User ( IList list1<User>=new
List<User>() ) and where User implement the interface IUser.
But this did not work...
I thought that as User implement IUser it was possible to cast the generic
collection between the two types. Currently I ve to use a foreach loop to
populate the list2 collection :
foreach(User u in list1)
{
list2.add(u as IUser);
}

Is there a more efficient and elegant way for doing this ?

Thanks.
Yalt.


Apr 14 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7955
by: conchur | last post by:
Is there an elegant way to have a class utilise an inherited method to return a Typed Collection of itself? Example: class MyList<T> : List<T> where T : MyRoot { Add... Remove... }
3
2756
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some dead ends. I'm posting this to get some feedback on wether I'm going in the right direction, and at the same time hopefully save others from going through the process.
4
3236
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a subclass of Logical (LogicalA, LogicalB). I'm currently casting them to the right type when I retrieve them, but obviously this isn't terribly 'nice'. I tried converting the class to be generic, and having a type parameter that affected the type of...
9
5839
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
10
2934
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a generic list of business objects. I'm able to see that the type is the correct one in the debugger. However when I try to traverse the list using the type I can't compile. The same type variable I've verified as being passed
4
7835
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have written a generic method which does different things depending on the type of the parameter. I got it to work, but it seems really inelegant. Is there a better way to do this? In the code below, ColumnMap is a simple struct which basically has three properties, Header (a string), Index (an int), TypeOfData (which is a DataType which is a local eNum). _Mapping is a local List<ColumnMap>. public ColumnMap GetMap<T>(T value)
3
4756
by: daokfella | last post by:
I simply want to extend the List<Tobject to include a property called VirtualCount. Basically, the list will hold my paged records, but the VirtualCount property will tell me how many records total are in the result set. It seems easy enough to create my class: public class EntityList<T: List<T> { public int VirtualCount { get; set; } }
8
3145
by: MMAS | last post by:
Hey everyone -- Curious about some strange behaviour I'm seeing that seems to be related to my lack of understanding on how generics work in C#. Here's some simplified code (sorry for strange formatting) to show what my issue is. I have a generic abstract class here: public abstract class MyGenericAbstractClass<Twhere T : MyInterface
2
4178
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of types derived from that base type, or arrays or generic collections of instances of types derived from that base type. All is well until I come to the properties which are generic collections, I don't seem to be able to find an elegant way of...
0
8297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8717
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5629
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.