473,473 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Generics and inheritance

Hi,

Let's say I have 2 classes, one of which is inheriting from the other.
I also have a generic class.
When I declare an instance of the generic class, I want to be able to cast a
generic declaration of the child class as the base class.

I have an example below, and the last line in TestSub does not compile.
Since Class2 IS a Class1, I want to be able to make that assignment, and
treat my collection of Class2 objects as Class1 objects if I want, since
after all, that is what they are.

How do I go about making this work? Is it event possible?

Public Class Class1
End Class

Public Class Class2
Inherits Class1
End Class

Public Class CollectionClass(Of someType)
End Class

Public Class TestClass
Public Sub TestSub()
Dim t1 As CollectionClass(Of Class1)
Dim t2 As New CollectionClass(Of Class2)

t1 = t2
End Sub
End Class
Dec 22 '05 #1
6 1217
"Marina" <so*****@nospam.com> schrieb:
Let's say I have 2 classes, one of which is inheriting from the other.
I also have a generic class.
When I declare an instance of the generic class, I want to be able to cast
a generic declaration of the child class as the base class.
[...] How do I go about making this work? Is it event possible?


That's not possible. There is no subtype-relationship between 'Foo(Of
Derived)' and 'Foo(Of Base)'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 22 '05 #2
Why not? Anything you can do to Foo(of Derived) in a generic manner, you can
do to Foo(Of Base)

Even if the collection is declared as CollectionClass(Of someType As Base),
this still does not compile. In this case, it would be guaranteed that only
Base and its children can be instantiated. If I have an instance of
Derived, I can treat it as a Base. So if I have a Foo(Of Derived), why can't
I treat it as Foo(Of Base)?

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OH**************@tk2msftngp13.phx.gbl...
"Marina" <so*****@nospam.com> schrieb:
Let's say I have 2 classes, one of which is inheriting from the other.
I also have a generic class.
When I declare an instance of the generic class, I want to be able to
cast a generic declaration of the child class as the base class.
[...] How do I go about making this work? Is it event possible?


That's not possible. There is no subtype-relationship between 'Foo(Of
Derived)' and 'Foo(Of Base)'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 22 '05 #3
Marina,

"Marina" <so*****@nospam.com> schrieb:
Why not? Anything you can do to Foo(of Derived) in a generic manner, you
can do to Foo(Of Base)

Even if the collection is declared as CollectionClass(Of someType As
Base), this still does not compile. In this case, it would be guaranteed
that only Base and its children can be instantiated. If I have an
instance of Derived, I can treat it as a Base. So if I have a Foo(Of
Derived), why can't I treat it as Foo(Of Base)?


I do not know the exact reasons for this behavior, but has clearly been a
design decision.

However, note that 'BaseCollection(Of Class1)' is the base type of
'DerivedCollection(Of Class1)' if 'DerivedCollection(Of T)' inherits from
'BaseCollection(Of T)'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 22 '05 #4
Why not? Anything you can do to Foo(of Derived) in a generic manner, you can
do to Foo(Of Base)


But not the other way around, i.e. there are things you can do with
your Foo(Of Base) that shouldn't be allowed for a Foo(Of Derived). To
continue your example, imagine if you write

Public Sub TestSub()
Dim t1 As CollectionClass(Of Class1)
Dim t2 As New CollectionClass(Of Class2)

t1 = t2
t1.Add(New Class1)
End Sub

Adding a Class1 instance to a Class2 collection is clearly a no no but
the code would compile and you'd get a runtime error instead. That
would go against one of the major benefits with generics - to get
improved compile time type safety.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 22 '05 #5
Ok, fair point.

However, it would be awfully nice to have strongly typed collection classes
that can be cast back and forth the same way.

In reality, I want to have a Class1Collection and a Class2Collection where
each one is coded to return the corresponding class type, but where
Class2Collection is derived from Class1Collection. Right now it's a lot of
repetitive code with shadowed properties and such, and I was hoping generics
would be the way to eliminate all that repetition.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Why not? Anything you can do to Foo(of Derived) in a generic manner, you
can
do to Foo(Of Base)


But not the other way around, i.e. there are things you can do with
your Foo(Of Base) that shouldn't be allowed for a Foo(Of Derived). To
continue your example, imagine if you write

Public Sub TestSub()
Dim t1 As CollectionClass(Of Class1)
Dim t2 As New CollectionClass(Of Class2)

t1 = t2
t1.Add(New Class1)
End Sub

Adding a Class1 instance to a Class2 collection is clearly a no no but
the code would compile and you'd get a runtime error instead. That
would go against one of the major benefits with generics - to get
improved compile time type safety.
Mattias

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

Dec 22 '05 #6
| Why not? Anything you can do to Foo(of Derived) in a generic manner, you
can
| do to Foo(Of Base)
Rick Byers goes into more details on the reason Mattias gave:

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

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Marina" <so*****@nospam.com> wrote in message
news:uE**************@TK2MSFTNGP12.phx.gbl...
| Why not? Anything you can do to Foo(of Derived) in a generic manner, you
can
| do to Foo(Of Base)
|
| Even if the collection is declared as CollectionClass(Of someType As
Base),
| this still does not compile. In this case, it would be guaranteed that
only
| Base and its children can be instantiated. If I have an instance of
| Derived, I can treat it as a Base. So if I have a Foo(Of Derived), why
can't
| I treat it as Foo(Of Base)?
|
| "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
| news:OH**************@tk2msftngp13.phx.gbl...
| > "Marina" <so*****@nospam.com> schrieb:
| >> Let's say I have 2 classes, one of which is inheriting from the other.
| >> I also have a generic class.
| >> When I declare an instance of the generic class, I want to be able to
| >> cast a generic declaration of the child class as the base class.
| >> [...] How do I go about making this work? Is it event possible?
| >
| > That's not possible. There is no subtype-relationship between 'Foo(Of
| > Derived)' and 'Foo(Of Base)'.
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
|
|
Dec 22 '05 #7

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...
5
by: PeterLawrance | last post by:
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...
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
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
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...
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
0
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 ...
0
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...

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.