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 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());
}
}
"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;
}
}
Thanks you Jon , Thanks you Alberto
a fantastic piece of information I was missing !
:-)
Thanks a lot
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |