Generics and multi-level inheritance

D

Daniel Billingsley

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.ChildClass>' to match
overridden member
'GenericsLibrary.RootClass<GenericsLibrary.MiddleClass<GenericsLibrary.ChildClass>>.GetOne()'
E:\Development\Projects\TempJunk\GenericsTest\GenericsLibrary\ChildClass.cs
10 30 GenericsLibrary


How do I accomplish what I want, or what am I misunderstanding about the use
of generics?
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
D

Daniel Billingsley

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 said:
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]
- (e-mail address removed)

Daniel Billingsley said:
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.ChildClass>' to match
overridden member
'GenericsLibrary.RootClass<GenericsLibrary.MiddleClass<GenericsLibrary.ChildClass>>.GetOne()'
E:\Development\Projects\TempJunk\GenericsTest\GenericsLibrary\ChildClass.cs
10 30 GenericsLibrary


How do I accomplish what I want, or what am I misunderstanding about the
use of generics?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top