473,567 Members | 2,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<IBas eItemItems{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 IAnotherBaseIte m : IBaseItem
{
string OtherStuff { get; set; }
}

The question comes down to implementation. When implementing the second set
of classes the Collection<shou ld be of IAnotherBaseIte m 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 IAnotherBaseIte m class.

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

class Stuff : IAnotherBase
{
private Collection<IAno therBaseItem_it ems;

// this is the interface from IBase
public Collection<IBas eItemItems
{
// 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
IAnotherBaseIte m or something else?

Anything that anybody can help me with would be great.

Thanks.
Sep 9 '06 #1
7 2087
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<IBa seItem>)this._i tems; }
set { this._items = (Collection<IAn otherBaseItem>) 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*********@ho tmail.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<IBa seItem>)this._i tems; }
set { this._items = (Collection<IAn otherBaseItem>) 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.co m>
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*********@ho tmail.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.co m>
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*********@ho tmail.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.co m>
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
3980
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 looked at the files in the Gyro download but I couldn't find any mention of constraints. Can anyone enlighten me what the current status is and what we...
17
3300
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 way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even...
12
2726
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 it defines the language more completely. Great to use.
4
1412
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 called Animals. I'd like to have a class: Collection<Animals>
7
2195
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
1833
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 interface or something like that, but I didn't find. public static int MaxSum(int source) { int maxSoFar = 0, maxEndingHere = 0;
6
5444
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 type constraint 'System.Windows.Forms.ComboBox' must come before any other constraints If I switch place of ComboBox with TextBoxBase, it would...
3
2720
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 OLE Object (load a picture from a given path). Something like this I would expect: _reports.crImage local_Report = new _reports.crImage();
3
1294
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 current non-generic implementation is functional, but not as clean as I would like. Although not sure, I believe my problems stem from lacking...
0
7688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7905
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8115
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...
1
7651
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...
0
7960
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...
0
6272
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3648
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...
1
1205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
930
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...

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.