471,350 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

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 4061
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Sahil Malik | last post: by
23 posts views Thread by Luc Vaillant | last post: by
7 posts views Thread by Sean | last post: by
9 posts views Thread by sloan | last post: by
2 posts views Thread by veeru | last post: by
13 posts views Thread by rkausch | last post: by

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.