473,382 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1097
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

14
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
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
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
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
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
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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.