473,406 Members | 2,698 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,406 software developers and data experts.

Generic type implementers, class and interface versions not compatible

HC
I've stripped down my code to barebones to demonstrate (below).

I have interfaces and classes which implement them. I force my generic
collection to accept only the interface type. If I create an instance
of the generic class using the class implemention of the interface I
find it can't be converted to the interface version, and vice versa.
This a pain with my current design.

Can someone please advise on what best practice might be in this
situation. Are there any cardinal rules or similar that I'm violating,
passing an interface to the generic collection instead of an actual
class?

The compiler should know they both support the same interface so why
not compatible?

thanks

interface IMyInterface
{
}

class ImplementsMyInterface : IMyInterface
{
}

class GenericClass<T> where T : IMyInterface
{
}

class TestAbove
{
void MethodWhichTakesClass(GenericClass<ImplementsMyInt erface> ts)
{
MethodWhichTakesInterface(ts); // Cannot convert type
'Test.GenericClass<Test.ImplementsMyInterface>' to
'Test.GenericClass<Test.IMyInterface>'
}
void MethodWhichTakesInterface(GenericClass<IMyInterfac e> ts)
{
MethodWhichTakesClass(ts); // cannot convert from
'Test.GenericClass<Test.IMyInterface>' to
'Test.GenericClass<Test.ImplementsMyInterface>'
}
}

Feb 14 '06 #1
1 1589
HC
I might know what I have to do.

I have child interfaces which inherit a parent interface, and class
implementations. The parent class/interface is what I use as a
parameter to the generic collection, using the interface where I want
it to store all inherited types.

I don't actually inherit from the parent class in my child classes,
only from the child interfaces (which inherit the parent), so I'm
implementing the parent independently in each child.

Maybe I need to inherit child classes from the parent class, and then I
might be able to get away with using the parent class always in my
generic collection.

???

Feb 14 '06 #2

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

Similar topics

49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
4
by: Jethro Guo | last post by:
C++ template use constraint by signature,It's very flexible to programmer but complex for complier, and at most time programmer can not get clear error message from complier if error occur. C#...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
3
by: BombDrop | last post by:
I'm working on a system that has 4 different types of client they have mostly the same attributes so I decided to create an interface IClient for them to implement and then just add the differing...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
10
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another...
4
by: tadmill | last post by:
Hi, Is it possible for a generic list class to use Reflection on the generic type being used to detect a property within that type that refers to the same generic class, only with a different...
7
by: tadmill | last post by:
Is it possible for a class that accepts a generic type, to re-create another instance of itself with a property type of the passed class? ex. public class SomeClass<T> { private PropertyInfo...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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
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
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...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.