473,396 Members | 2,037 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,396 software developers and data experts.

Generics and Inheritance

Hi,

I would expect that inheritance through the parameters of a generic class
would be fine however I get errors as below in the following code.

Public Class Form1
Sub test()
Dim xxx As Foo(Of DerivedBase)
Dim zzz As Foo(Of Base)

zzz = xxx '*** Foo(DerivedBase) cannot be converted to Foo(Base)
zzz = CType(xxx, Foo(Of Base)) '*** Foo(DerivedBase) cannot be
converted to Foo(Base)

Dim aaa As Bar(Of Base)
zzz = aaa
End Sub
End Class

Public Class Foo(Of XXX As Base)
End Class

Public Class DerivedBase
Inherits Base
End Class

Public Class Base
End Class

Any (clean) workarounds?
Nov 21 '05 #1
5 1147
>Any (clean) workarounds?

I'm not sure what kind of workaround you're looking for. But you can
use just a Foo(Of Base) for both Base and DerivedBase instances. Of
course that means you have to cast whenever you need to use
DerivedBase specific functionality.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
Yes I can cast however a substantial part of the raison d'etre for generics
is that we remove casting n'est pas? And if I'm going to cast anyway why add
the complexity of generics, it could be implemented as a straight hierarchy
from Base with a Foo interface. That however fixes the implementation and I
lose any chance to reuse the code for other purposes.

I guess my point is that I can see no reason to preclude the syntax and it
does make the implementation cleaner, and it certainly makes for better code
reuse, so why disallow it? It appears the compiler is naively comparing
Foo(Of Base) and Foo(Of DerivedBase) and saying there is no relationship;
whereas there truly is an inheritance relationship just as there is an
inheritance relationship between Base and DerivedBase.

The utility is enhanced if we introduce another class Derived2Base which is
also derived from Base. I can then use a reference to Foo(Of Base) for either
one which is the generics equivalent of being able to use a fruit reference
to access apples or bananas.
"Mattias Sjögren" wrote:
Any (clean) workarounds?


I'm not sure what kind of workaround you're looking for. But you can
use just a Foo(Of Base) for both Base and DerivedBase instances. Of
course that means you have to cast whenever you need to use
DerivedBase specific functionality.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #3
Yes I can cast however a substantial part of the raison d'etre for generics
is that we remove casting n'est pas?
Oui.

I guess my point is that I can see no reason to preclude the syntax and it
does make the implementation cleaner, and it certainly makes for better code
reuse, so why disallow it?
I suggest you read Rick Byers' discussion of the pros and cons of type
parameter variance here

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

It appears the compiler is naively comparing
Foo(Of Base) and Foo(Of DerivedBase) and saying there is no relationship;
whereas there truly is an inheritance relationship just as there is an
inheritance relationship between Base and DerivedBase.


No, there isn't. It can be simulated, like they do today with arrays.
But a List(Of DerivedBase) doesn't really inherit from List(Of Base).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #4
A popular misconception is the only real purpose of generics is to support
collections. And given the implementation in Beta2 I agree that allowing what
I would like to do will cause programs to break in non-trivial ways. A closer
look should be paid to the Java implementation rather than aiming low for a
nice textbite on the box. Put me down for front row tickets to see something
more upmarket in the final release ... or is it that the grass really is
greener where the Sun shines?

"Mattias Sjögren" wrote:
Yes I can cast however a substantial part of the raison d'etre for generics
is that we remove casting n'est pas?


Oui.

I guess my point is that I can see no reason to preclude the syntax and it
does make the implementation cleaner, and it certainly makes for better code
reuse, so why disallow it?


I suggest you read Rick Byers' discussion of the pros and cons of type
parameter variance here

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

It appears the compiler is naively comparing
Foo(Of Base) and Foo(Of DerivedBase) and saying there is no relationship;
whereas there truly is an inheritance relationship just as there is an
inheritance relationship between Base and DerivedBase.


No, there isn't. It can be simulated, like they do today with arrays.
But a List(Of DerivedBase) doesn't really inherit from List(Of Base).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #5
>A closer look should be paid to the Java implementation rather than aiming low for a
nice textbite on the box.
What's so great about it? Last I read, it seemed like a big ugly hack
that didn't provide any real type safety of performance improvement.

Put me down for front row tickets to see something
more upmarket in the final release ...


I wouldn't expect any changes related to this between beta 2 and RTM.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #6

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

Similar topics

3
by: Eddie | last post by:
Hi, I've recently read the articles on generics in the .net framework (the next version) and just wanted to check a few points.. As I understand, generic class instances while being strongly...
3
by: Pierre Y. | last post by:
Hi, I've just read that article : http://msdn.microsoft.com/library/en-us/dnvs05/html/csharp_generics.asp I'm asking something. Why are generics constraints not been implemented using...
6
by: Thomas Tomiczek | last post by:
Ok, working my way through a complex library conversion to .NET 2.0 and C# 2.0 with generics I am totally stuck on one thing -if anyone sees the issue I would be more than glad. The situation is...
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...
2
by: Daniel Billingsley | last post by:
Let's say I have: public class RootClass<T> { public virtual T GetOne() { return default(T); } }
5
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
8
by: Kris Jennings | last post by:
Hi, I am trying to create a new generic class and am having trouble casting a generic type to a specific type. For example, public class MyClass<Twhere T : MyItemClass, new() { public...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...
0
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,...

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.