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 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/
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/
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>();
}
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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.
|
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;
};
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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 projectplanning, coding, testing,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| | |