Express formally Inheritance in a class with parameters - Either C# or VB

P

pamela fluente

I have a question: either a C# or VB answer is perfectly fine. I want
to know if there is a way to tell a class that there is an inheritance
relationship between the type parameters.

I have a complex situation, but to make it easy I have reduced the
problem to the very essence.
Here is my simple example (vb syntax):

------------------------------------

Class ParentType
End Class

Class DerivedType
Inherits ParentType
End Class

Class AnyType
End Class

Class Example(Of AnyType, DerivedType, ParentType)

Public P As ParentType

Function GetChild() As DerivedType
Return DirectCast(P, DerivedType)
End Function

End Class

------------------------------------


The point here is that the instruction

DirectCast(P, DerivedType)

would be ok if I had a way to "inform" the class Class Example(Of
DerivedType, ParentType) that
"DerivedType" will be inheriting "ParentType".

(for instance, this would be remarkably useful to avoid casting when
using "GetChild" class.)

How can I do that?

-P
 
A

Alberto Poblacion

pamela fluente said:
either a C# or VB answer is perfectly fine
[...]
The point here is that the instruction

DirectCast(P, DerivedType)

would be ok if I had a way to "inform" the class Class Example(Of
DerivedType, ParentType) that
"DerivedType" will be inheriting "ParentType".

You can inform the compiler of the dependency between classes by means
of a restriction on the Generic parameters ("where" clause in the class
definition):

class example<AnyType, DerivedType, ParentType> where DerivedType :
ParentType
{
public ParentType P;
public DerivedType GetChild()
{
return (DerivedType)P;
}
}
 
J

Jon Skeet [C# MVP]

I have a question: either a C# or VB answer is perfectly fine. I want
to know if there is a way to tell a class that there is an inheritance
relationship between the type parameters.

Yes, assuming I understand your question correctly:

using System;

class Parent
{
}

class Child : Parent
{
}

class Generic<P,C> where C : P, new()
{
public P CreateChildAsParent()
{
return new C();
}
}

class Foo
{
static void Main()
{
Generic<Parent,Child> x = new Generic<Parent,Child>();
Parent p = x.CreateChildAsParent();
Console.WriteLine (p.GetType());
}
}
 
P

pamela fluente

Thanks you Jon , Thanks you Alberto

a fantastic piece of information I was missing !

:)


Thanks a lot
 
P

pamela fluente

Thanks you Jon , Thanks you Alberto

a fantastic piece of information I was missing !

:)

Thanks a lot

BTW for those interested the VB syntax would be:

Class example(Of AnyType, DerivedType As ParentType, ParentType)
Public P As ParentType
Public Function GetChild() As DerivedType
Return DirectCast(P, DerivedType)
End Function
End Class
 

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