473,394 Members | 1,870 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,394 software developers and data experts.

Generics with inherited Interfaces.

I have a question about using Generics with Interfaces and some of there
inheritance issues / problems. If this is not possible what I describe below
I will have to go a different route and would like some suggestions.

I am unable to use abstract classes as my code must not effect any current
inheritance chains so I am using interfaces. So I have something like this:

interface IBase
{
Collection<IBaseItemItems{get;set;}
}

interface IBaseItem
{
string Title { get; set; }
}

Now that is the base and then we will have components that will extend this:

interface IAnotherBase : IBase
{
string Information {get; set; }
}

interface IAnotherBaseItem : IBaseItem
{
string OtherStuff { get; set; }
}

The question comes down to implementation. When implementing the second set
of classes the Collection<should be of IAnotherBaseItem elements. So I
need to be able to cast this stuff out. As well any class that implements
the IAnotherBase class should enforce that the items being added to the
collection are of the correct type of IAnotherBaseItem class.

In trying to do something like this it won't let me:

class Stuff : IAnotherBase
{
private Collection<IAnotherBaseItem_items;

// this is the interface from IBase
public Collection<IBaseItemItems
{
// This fails
get { return this._items; }
set { this._items = value; }
}
}

What I need to be able to do is something I guess like interface shadowing
however I need to figure out how to cast this information out. Do I need to
use a secondary internal Collection<of the IBaseItem as well as the
IAnotherBaseItem or something else?

Anything that anybody can help me with would be great.

Thanks.
Sep 9 '06 #1
7 2075
I haven't run this code to test if its right, but since you no doubt
have an example there, have you tried casting? Eg.
get { return this._items; }
set { this._items = value; }
becomes
get { return (Collection<IBaseItem>)this._items; }
set { this._items = (Collection<IAnotherBaseItem>)value; }
I'm not sure if you can even cast with generics this way. But I just
thought I'd suggest what seemed most evident to me. Let me know result.

Cheers,
Steven Nagy

Sep 10 '06 #2
Steven Nagy <le*********@hotmail.comwrote:
I haven't run this code to test if its right, but since you no doubt
have an example there, have you tried casting? Eg.
get { return this._items; }
set { this._items = value; }

becomes
get { return (Collection<IBaseItem>)this._items; }
set { this._items = (Collection<IAnotherBaseItem>)value; }

I'm not sure if you can even cast with generics this way. But I just
thought I'd suggest what seemed most evident to me. Let me know result.
No, you can't. For instance, List<stringcan't be cast to
List<object>. This is because you would then be able to add an object
to the List<objectand that would violate it being a List<string>.

To put it in a nutshell which may or may not help, C# does not expose
generic covariance or contravariance. If you search with the
appropriate terms, you'll find much longer explanations :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 10 '06 #3
Yeah I guess it makes sense to me. I mean, whats the point of trying to
make generics, MORE generic by removing the whole type-safety? For me,
I find it great to get a design-time, strongly typed object out of a
"generic" situation, and to do what I suggested before is as bad as
just having a non-generic collection of "object"s and casting. It
totally removes the point of having a generic collection. I totally
take your point. And I'll check those terms... wikipedia?

Also, when's your next planned BLOG post?

Sep 10 '06 #4
Steven Nagy <le*********@hotmail.comwrote:
Yeah I guess it makes sense to me. I mean, whats the point of trying to
make generics, MORE generic by removing the whole type-safety? For me,
I find it great to get a design-time, strongly typed object out of a
"generic" situation, and to do what I suggested before is as bad as
just having a non-generic collection of "object"s and casting. It
totally removes the point of having a generic collection. I totally
take your point. And I'll check those terms... wikipedia?
Have a look at:

http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx

http://www.dotnetgeeks.com/blogs/dot...07/31/CoVarian
ce_2C00_-Contravariance-and-Generics.aspx

http://www.dotnetgeeks.com/blogs/dot...08/09/Covarian
ce_2C00_-Contravariance-and-Generics-Part-Deux.aspx
Also, when's your next planned BLOG post?
I've got two posts "in the works" but I wouldn't like to say when
they'll be finished...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 11 '06 #5
Have a look at:
>
http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx

http://www.dotnetgeeks.com/blogs/dot...07/31/CoVarian
ce_2C00_-Contravariance-and-Generics.aspx

http://www.dotnetgeeks.com/blogs/dot...08/09/Covarian
ce_2C00_-Contravariance-and-Generics-Part-Deux.aspx

The first link was a good read. If I understood it correctly,
covariance is supported naturally by the CLR, but the specific language
compilers have disabled it?
Why not leave it in, and then let the programmer make the decision
about when its good or bad to use it?

The other 2 links didn't work for me, but I understand it better now.
I know you're a Java programmer as well, do you use covariant generics
much there?
(Sorry I don't know Java at all, just more interested in the demand).

SN

Sep 11 '06 #6
Steven Nagy <le*********@hotmail.comwrote:
Have a look at:

http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx
<snip>
The first link was a good read. If I understood it correctly,
covariance is supported naturally by the CLR, but the specific language
compilers have disabled it?
Yup. At least, the C# spec has effectively disabled it - other
languages could still include it, although I don't *think* C++ or
VB.NET do.
Why not leave it in, and then let the programmer make the decision
about when its good or bad to use it?
<snip>

Well, there's a bit of a difficulty in terms of the standard library -
that would really have to use it to make it useful elsewhere, and that
means the documentation would need to include references to it, which
means it's really "all or nothing" for the mainstream languages. It
*is* a pain to understand. I think I'd prefer it to be present as well,
on balance, but I can understand the decision on grounds of keeping
things simple.

As far as I know (I haven't looked in detail) .NET covariance only
applies to interfaces, too, which is a fairly major restriction. I do
wonder why it's in there at all, given all of these things...
I know you're a Java programmer as well, do you use covariant generics
much there?
I use it a fair amount, yes.
(Sorry I don't know Java at all, just more interested in the demand).
I think it's more obviously needed when it's missing :)

(It's one of the harder-to-understand bits of generics though - I
usually suggest people ignore it until they need it.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 11 '06 #7
As far as I know (I haven't looked in detail) .NET covariance only
applies to interfaces, too, which is a fairly major restriction. I do
wonder why it's in there at all, given all of these things...
That article you pointed me to implied it was only Interfaces that it
supports.

Thanks for the great info.

A common saying here in Australia is that when you learn something new,
you get to go home!

SN

Sep 12 '06 #8

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

Similar topics

11
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
4
by: Chuck Cobb | last post by:
I have a question regarding generics: Suppose I want to create some generic collection classes: Collection<Cats> c; Collection<Dogs> d; and both Cats and Dogs are inherited from a base class...
7
by: Ajeet | last post by:
hi I am having some difficulty in casting using generics. These are the classes. public interface IProvider<PROF> where PROF : IProviderProfile { //Some properties/methods }
32
by: Martin | last post by:
Hi all, In the following example, I'd like to replace int by a generic. In order word, I would like to replace int by float, double, byte or something like that. I've tried to find a common...
6
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class...
3
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have some problems with Crystal Reports (Version 10.2, Runtime 2.0). In Section3 I have added a OLE Object (Bitmap). Now when I open the report in my code I would like to set this...
3
by: Anders Borum | last post by:
Hello, I've worked on an API for quite some time and have (on several occasions) tried to introduce generics at the core abstract level of business objects (especially a hierarchical node). The...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.