473,493 Members | 2,245 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Generic classes, inheritance and polimorphism

OK, this was hard to explain on a short title. Sorry.

I have an abstract class (MustInherit) named Base, and a generic collection
class named ColBase which can only take Base and derived classes as it's
type.

I also have an indefinite number of classes that inherit Base. I want to
make a method on Base that returns a ColBase(of Base), but instead of
ColBase(of Base) use the derived class.

So if I have a class named Inherited, when I call Inherited.GetColBase it
returns a ColBase(of Inherited) and not a ColBase(of Base).

Do you know if this is possible, and if so, how to do it? Thanks a lot,

JoaquĆ*n Raya

Feb 21 '07 #1
3 4255
Joaquin wrote:
OK, this was hard to explain on a short title. Sorry.

I have an abstract class (MustInherit) named Base, and a generic collection
class named ColBase which can only take Base and derived classes as it's
type.

I also have an indefinite number of classes that inherit Base. I want to
make a method on Base that returns a ColBase(of Base), but instead of
ColBase(of Base) use the derived class.

So if I have a class named Inherited, when I call Inherited.GetColBase it
returns a ColBase(of Inherited) and not a ColBase(of Base).
It sounds like what you are looking for is generic type argument
covariance. C# does not support it, but .NET does support it when the
type parameter appears as a type for a return value only (covariance is
only safe as an "output-only" or return value, similarly contravariance
is only safe as an input-only argument).

Short answer, you'll have to create specific methods in the
descendant(s) that explicitly cast from Base to Inherited.

-- Barry

--
http://barrkel.blogspot.com/
Feb 21 '07 #2
Well, thanks for your answer. It's a pity, it would have been great.

"Barry Kelly" wrote:
Joaquin wrote:
OK, this was hard to explain on a short title. Sorry.

I have an abstract class (MustInherit) named Base, and a generic collection
class named ColBase which can only take Base and derived classes as it's
type.

I also have an indefinite number of classes that inherit Base. I want to
make a method on Base that returns a ColBase(of Base), but instead of
ColBase(of Base) use the derived class.

So if I have a class named Inherited, when I call Inherited.GetColBase it
returns a ColBase(of Inherited) and not a ColBase(of Base).

It sounds like what you are looking for is generic type argument
covariance. C# does not support it, but .NET does support it when the
type parameter appears as a type for a return value only (covariance is
only safe as an "output-only" or return value, similarly contravariance
is only safe as an input-only argument).

Short answer, you'll have to create specific methods in the
descendant(s) that explicitly cast from Base to Inherited.

-- Barry

--
http://barrkel.blogspot.com/
Feb 21 '07 #3
this should be a valid workaround... the compiler may complain if
your generic collection class has a constraint on it (ie.. T must be a
subclass of Base).. in that case you just need to add the constraint
on the generic methods as well

public abstract class Base()
{
protected IList<TGetNewList() where T : Base
{
return CreateList(this);
}

private IList<TCreateList(T objectToCreateListFor) where T : Base
{
return new List<T>();
}
}

Mar 1 '07 #4

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

Similar topics

18
3007
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
22
13087
by: Adam Clauss | last post by:
OK, I have class A defined as follows: class A { A(Queue<B> queue) { ... } } Now, I then have a subclass of both classes A and B. The subclass of A (SubA), more specifically is passed a...
2
1515
by: Harold Howe | last post by:
Howdy all, I am getting a compiler error regarding a consrained conversion. It complains that it can't make the type conversion, even though the generic type argument inherits from the target of...
16
2331
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
25
2994
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
28
2591
by: steve yee | last post by:
i think c should adapt c++ template standard, as well as namespace. if so, c can replace c++ in many cases.
23
4575
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
4
7550
by: =?Utf-8?B?QkogU2FmZGll?= | last post by:
We have a class that has a public property that is of type List<T>. FXCop generates a DoNotExposeGenericLists error, indicating "System.Collections.Generic.List<Tis a generic collection designed...
9
5829
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
0
6989
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
7157
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,...
1
6873
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
7367
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
5453
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,...
0
4579
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
3088
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
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
285
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.