473,804 Members | 3,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ImplementsMyInt erface : IMyInterface
{
}

class GenericClass<T> where T : IMyInterface
{
}

class TestAbove
{
void MethodWhichTake sClass(GenericC lass<Implements MyInterface> ts)
{
MethodWhichTake sInterface(ts); // Cannot convert type
'Test.GenericCl ass<Test.Implem entsMyInterface >' to
'Test.GenericCl ass<Test.IMyInt erface>'
}
void MethodWhichTake sInterface(Gene ricClass<IMyInt erface> ts)
{
MethodWhichTake sClass(ts); // cannot convert from
'Test.GenericCl ass<Test.IMyInt erface>' to
'Test.GenericCl ass<Test.Implem entsMyInterface >'
}
}

Feb 14 '06 #1
1 1616
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
2903
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 class). Any comments would be appreciated! Thanks! Steve ----------------------------------------------------------------------
17
3334
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 way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even impossible to write truly generic code. For example, it seems to be impossible to write a truly generic
4
2047
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# generic use constraint by type,complier is relaxed, but it is very limited to programmer.Is there a way to get merits of both? Maybe the following way can achieve this purpose : //First add a keyword "constrant" to modify class or struct just...
9
12856
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 implements IQueue<Tfor all types T (so it can make use of queues of various object types internally). As useful as this is, it doesn't seem possible. The natural (but illegal) notation would be something like class A<QueueClasswhere QueueClass :...
3
1747
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 attributes as needed to each client class. Now I get the Client information from a DB and Load them into a Generic dictionary
13
3839
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 application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The problem I'm running into has to do with the differences in implementations of Generics between the two...
10
2715
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 class that contains an object of type T) I have a third generic class: MyHandler<Twhich contains 2 Add methods and a generic method: Add(T obj)
4
2300
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 type as the parameter? From various posts and help, this is what I've got so far: public class DataList<T: BindingList<T>
7
1751
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 propInfos; private Type objectType = typeof(T); public SomeClass()
0
9584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10583
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10337
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9160
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6854
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
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 we have to send another system
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.