473,511 Members | 15,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generics and inheritance

Sorry if this is the wrong group, but I cna think not of where else to post.

I have an inheritance hierarchy in place as follows ...

PersonVisitor<T<-- AlphabeticVisitor<T<-- FamilyNameVisitor<T>

public interface IPerson
{
string GivenName { get; set; }
string FamilyName { get; set; }
IPerson Clone();
void Visit(PersonVisitor<IPersonvisitor);
}

public abstract class PersonVisitor<Twhere T : IPerson
{
virtual public void Visit(T c) { }
}

public abstract class AlphabeticVisitor<T: PersonVisitor<Twhere T :
IPerson
{
// class code omited
}

public class FamilyNameVisitor<T: AlphabeticVisitor<Twhere T : IPerson
{
override public void Visit(T c)
{
}
}

Firstly, I would expect that I could pass an object of type
FamilyNameVisitor<Tas follows

Customer c = PeopleFactory.CreateNewCustomer("Marcus", "Barnard");
Assert.IsNotNull(c, "Failed to instantiate the Customer object");
FamilyNameVisitor<Customerv = new FamilyNameVisitor<Customer>();
c.Visit(v);

However, I am told that this is not possible because an object of type
FamilyNameVisitor<Customercannot be converted to type
PersonVisitor<IPerson(even though FamilyNameVisitor<Customeris a
sub-class of PersonVisitor<IPerson>, AND Customer is a sub-class of IPerson)

So, I then put the following implicit conversion into the FamilyNameVisitor
class definition

public static implicit operator
PersonVisitor<IPerson>(FamilyNameVisitor<Tv)
{
return (v as PersonVisitor<IPerson>);
}

but this conversion always returns null :o(

Can somebody please help?

Thanks in advance

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk
Nov 19 '07 #1
1 1163
>However, I am told that this is not possible because an object of type
>FamilyNameVisitor<Customercannot be converted to type
PersonVisitor<IPerson(even though FamilyNameVisitor<Customeris a
sub-class of PersonVisitor<IPerson>, AND Customer is a sub-class of IPerson)
FamilyNameVisitor<Customeris *not* a subclass of
PersonVisitor<IPerson>. In your case I think the easiest solution is
to make the Visit method generic as well

void Visit<T>(PersonVisitor<Tvisitor) where T : IPerson
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 19 '07 #2

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

Similar topics

3
2000
by: Eddie | last post by:
Hi, I've recently read the articles on generics in the .net framework (the next version) and just wanted to check a few points.. As I understand, generic class instances while being strongly...
3
1467
by: Pierre Y. | last post by:
Hi, I've just read that article : http://msdn.microsoft.com/library/en-us/dnvs05/html/csharp_generics.asp I'm asking something. Why are generics constraints not been implemented using...
6
1958
by: Thomas Tomiczek | last post by:
Ok, working my way through a complex library conversion to .NET 2.0 and C# 2.0 with generics I am totally stuck on one thing -if anyone sees the issue I would be more than glad. The situation is...
5
1150
by: PeterLawrance | last post by:
Hi, I would expect that inheritance through the parameters of a generic class would be fine however I get errors as below in the following code. Public Class Form1 Sub test() Dim xxx As...
12
2715
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
2
4218
by: Daniel Billingsley | last post by:
Let's say I have: public class RootClass<T> { public virtual T GetOne() { return default(T); } }
5
2910
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
8
2408
by: Kris Jennings | last post by:
Hi, I am trying to create a new generic class and am having trouble casting a generic type to a specific type. For example, public class MyClass<Twhere T : MyItemClass, new() { public...
8
3158
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
6
5432
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class...
0
7252
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
7371
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,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
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
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
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 ...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.