473,508 Members | 2,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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

Apr 11 '07 #1
4 964
On Apr 11, 9:03 am, "pamela fluente" <pamelaflue...@libero.itwrote:
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,Cwhere C : P, new()
{
public P CreateChildAsParent()
{
return new C();
}
}

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

Apr 11 '07 #2
"pamela fluente" <pa***********@libero.itwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
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, ParentTypewhere DerivedType :
ParentType
{
public ParentType P;
public DerivedType GetChild()
{
return (DerivedType)P;
}
}
Apr 11 '07 #3
Thanks you Jon , Thanks you Alberto

a fantastic piece of information I was missing !

:-)
Thanks a lot

Apr 11 '07 #4
On 11 Apr, 12:22, "pamela fluente" <pamelaflue...@libero.itwrote:
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

Apr 12 '07 #5

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

Similar topics

11
3224
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
14
6378
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this...
45
6309
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
2
1625
by: yaniv.golan | last post by:
Hi, Either I am missing something, or there is a compiler bug in the way inheritance from generic s is treated. In the following code, class c2 inherits from c1_generic. c1_generic has T as a...
4
1201
by: RThaden | last post by:
Hi all, I looked in several books, articles, etc. but did not find a solution to my problem. Maybe somebody out there can help a desperate, not toooo experienced programmer: I want to...
7
3716
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
12
2223
by: Light | last post by:
Hi all, I posted this question in the sqlserver.newusers group but I am not getting any response there so I am going to try it on the fine folks here:). I inherited some legacy ASP codes in my...
4
1102
by: pamela fluente | last post by:
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...
6
2532
by: hsmit.home | last post by:
Hello, I came across a strange error and it's really been bugging me. Maybe someone else has come across this and any insight would be appreciated. What I'm trying to accomplish is using...
7
1766
by: barbaros | last post by:
Hello everybody, I am building a code for surface meshes (triangulations for instance). I need to implement Body objects (bodies can be points, segments, triangles and so on), then a Mesh will...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7127
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
7331
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,...
1
7054
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
7501
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...
0
4713
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
3204
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...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
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 ...

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.