473,486 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Generics and multi-level inheritance

Let's say I have:

public class RootClass<T>
{
public virtual T GetOne()
{
return default(T);
}
}

and

public class MiddleClass<T> : RootClass<MiddleClass<T>>
{
// A bunch of additional functionality
}

and

public class ChildClass : MiddleClass<ChildClass>
{
// Even more additional functionality
}

and I want to create the effect of this in ChildClass:

public override ChildClass GetOne()
{
// do some extra stuff
return base.GetOne();
}

so that ultimately when I'm using a ChildClass in a client class the return
type of GetOne() is untuitive and doesn't have to be cast (the point of the
generics in the first place).

I get this error on ChildClass.GetOne():

Error 1 'GenericsLibrary.ChildClass.GetOne()': return type must be
'GenericsLibrary.MiddleClass<GenericsLibrary.Child Class>' to match
overridden member
'GenericsLibrary.RootClass<GenericsLibrary.MiddleC lass<GenericsLibrary.ChildClass>>.GetOne()'
E:\Development\Projects\TempJunk\GenericsTest\Gene ricsLibrary\ChildClass.cs
10 30 GenericsLibrary
How do I accomplish what I want, or what am I misunderstanding about the use
of generics?
Jan 19 '06 #1
2 4213
Daniel,

You are misunderstanding it somewhat.

When you say on RootClass<T>:

public virtual T GetOne()

And then extend it with MiddleClass<T>, that declaration for GetOne
effectively becomes:

public virtual MiddleClass<T> GetOne()

Because you are replacing T with MiddleClass<T>.

Then, when you have ChildClass, it becomes:

public virtual MiddleClass<ChildClass> GetOne()

Which causes your error. I think what you want to do is have
MiddleClass declared as:

public class MiddleClass<T> : RootClass<T>

And then derive ChildClass from MiddleClass.

Also, as a side note, you really don't need "class" in your type name.
It's pretty much assumed. Prefixes such as "C" to indicate classes are also
discouraged.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daniel Billingsley" <Da***************@newsgroup.nospam> wrote in message
news:uc*************@TK2MSFTNGP15.phx.gbl...
Let's say I have:

public class RootClass<T>
{
public virtual T GetOne()
{
return default(T);
}
}

and

public class MiddleClass<T> : RootClass<MiddleClass<T>>
{
// A bunch of additional functionality
}

and

public class ChildClass : MiddleClass<ChildClass>
{
// Even more additional functionality
}

and I want to create the effect of this in ChildClass:

public override ChildClass GetOne()
{
// do some extra stuff
return base.GetOne();
}

so that ultimately when I'm using a ChildClass in a client class the
return type of GetOne() is untuitive and doesn't have to be cast (the
point of the generics in the first place).

I get this error on ChildClass.GetOne():

Error 1 'GenericsLibrary.ChildClass.GetOne()': return type must be
'GenericsLibrary.MiddleClass<GenericsLibrary.Child Class>' to match
overridden member
'GenericsLibrary.RootClass<GenericsLibrary.MiddleC lass<GenericsLibrary.ChildClass>>.GetOne()'
E:\Development\Projects\TempJunk\GenericsTest\Gene ricsLibrary\ChildClass.cs
10 30 GenericsLibrary
How do I accomplish what I want, or what am I misunderstanding about the
use of generics?

Jan 19 '06 #2
I understood all that except that

public class MiddleClass<T> : RootClass<T>

is what I really needed to do. Taking a while for the subtleties of
generics to sink in. :)

Thanks a million - I'm back off and running.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
Daniel,

You are misunderstanding it somewhat.

When you say on RootClass<T>:

public virtual T GetOne()

And then extend it with MiddleClass<T>, that declaration for GetOne
effectively becomes:

public virtual MiddleClass<T> GetOne()

Because you are replacing T with MiddleClass<T>.

Then, when you have ChildClass, it becomes:

public virtual MiddleClass<ChildClass> GetOne()

Which causes your error. I think what you want to do is have
MiddleClass declared as:

public class MiddleClass<T> : RootClass<T>

And then derive ChildClass from MiddleClass.

Also, as a side note, you really don't need "class" in your type name.
It's pretty much assumed. Prefixes such as "C" to indicate classes are
also discouraged.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daniel Billingsley" <Da***************@newsgroup.nospam> wrote in message
news:uc*************@TK2MSFTNGP15.phx.gbl...
Let's say I have:

public class RootClass<T>
{
public virtual T GetOne()
{
return default(T);
}
}

and

public class MiddleClass<T> : RootClass<MiddleClass<T>>
{
// A bunch of additional functionality
}

and

public class ChildClass : MiddleClass<ChildClass>
{
// Even more additional functionality
}

and I want to create the effect of this in ChildClass:

public override ChildClass GetOne()
{
// do some extra stuff
return base.GetOne();
}

so that ultimately when I'm using a ChildClass in a client class the
return type of GetOne() is untuitive and doesn't have to be cast (the
point of the generics in the first place).

I get this error on ChildClass.GetOne():

Error 1 'GenericsLibrary.ChildClass.GetOne()': return type must be
'GenericsLibrary.MiddleClass<GenericsLibrary.Child Class>' to match
overridden member
'GenericsLibrary.RootClass<GenericsLibrary.MiddleC lass<GenericsLibrary.ChildClass>>.GetOne()'
E:\Development\Projects\TempJunk\GenericsTest\Gene ricsLibrary\ChildClass.cs
10 30 GenericsLibrary
How do I accomplish what I want, or what am I misunderstanding about the
use of generics?


Jan 19 '06 #3

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

Similar topics

77
5207
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
6
374
by: Sahil Malik | last post by:
How come Generics are always represented by a single letter, usually T? Is that a language specification or just a common practice? - Sahil Malik You can reach me thru my blog at...
23
2517
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
2711
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...
7
4472
by: Sean | last post by:
I need a generic collection that will do the following key/value/value or index/value/value basically I need a multidimensional generic Collection if that is even possible!
9
5943
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
2
268
by: veeru | last post by:
Hi All, Can any one explain about the concept of Generics in C#
13
3789
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
5
5252
by: JC | last post by:
Hi all, I have scoured the internet for articles on sorting multi-dimensional arrays of generic types e.g. T using the IComparer<Tinterface and have run into a real roadblack. There does not...
4
3297
by: technomaget | last post by:
Hi, I am trying to implement a multi-purpose user control with generics. I can develop the generic class, and can instantiate concrete child classes inherited from the genric class, but have...
0
6964
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
7123
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
7175
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...
1
6842
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
7319
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...
1
4864
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
4559
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
1378
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 ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.