472,353 Members | 1,002 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 2018
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...
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...
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...
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...
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 :...
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...
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...
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...
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...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.