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

Deriving a more specific interface from a generic interface

Hi all - I'm sure I'm being brain-dead here - and I think the answer is 'you
can't do that', but I thought I would try.

I have an interface that is Generic, with two type arguments. I want to
create a second interface that specifices the second type argument. I also
want to have objects implement that second interface without having to
implement the more generic methods that are inherited from the first. This
is especially evident when I implement multiple interfaces:

Some code will illustrate the problem:

// Base Type
interface I
{}

// 'Base' manager
interface IBase<T1, T2> where T2 : ICollection<T1>
{
T2 GetT1s();
}

// Base type collection
interface ICol<T1> : ICollection<T1> where T1 : I {}

// Derived type
interface IDerived<T1> : IBase<T1, ICol<T1>> where T1 : I {}

// Actual entities to manage
class A : I {}
class B : I {}

// Manager
class Mgr : IDerived<A>, IDerived<B>
{
public ICol<A> GetT1s()
{...}

// Note the explicit implementation here
ICol<B> IBase<B, ICol<B>>.GetT1s()
{...}
}

I would really like to get rid of the 'IBase<B, ICol<B>>' - I would like to
use IDerived<B>, but this doesn't work. if I change IDerived to look like
this:

interface IDerived<T1> : IBase<T1, ICol<T1>> where T1 : I
{ new ICol<T1> GetT1s();}

Then I need to implement two forms of the method in the manager:

class Mgr : IDerived<A>, IDerived<B>
{
public ICol<A> GetT1s()
{...}

// I want the following method...
ICol<B> IDerived<B>.GetT1s()
{...}

// But not this one!
ICol<B> IBase<B, ICol<B>>.GetT1s()
{...}
}

What am I doing wrong? I would love to only have to implement
IDerived<B>.GetT1s();

Thanks,
Phil

Dec 15 '05 #1
2 1631
When you explicitly implement an interface method, you have to qualify
it with the name of the interface where it was defined. In your
example, that is IBase<B, ICol<B>>. IDerived<B> doesn't define any
methods, so you can't use it in an explicit implementation.

It is annoying, yes, but the VS2005 IDE will help by writing all the
explicit implementations for you with the correct base interface names.
For example, if you add IList<int> to a class's interface list, then
click the little box on the interface name and tell it to explicitly
implement the interface, it'll write some methods for IList<int>, some
for ICollection<int>, one for IEnumerable<int>, and one for
IEnumerable.

Jesse

Dec 16 '05 #2
Thanks Jesse - I was afraid of that. The problem really shows itself when
implement multiple interfaces - like when I use a partial class to define a
data provider (which I happen to be doing). Since some of the methods only
differ by return type, I have to define them explicity.

Thanks again,
Phil

"Jesse McGrew" wrote:
When you explicitly implement an interface method, you have to qualify
it with the name of the interface where it was defined. In your
example, that is IBase<B, ICol<B>>. IDerived<B> doesn't define any
methods, so you can't use it in an explicit implementation.

It is annoying, yes, but the VS2005 IDE will help by writing all the
explicit implementations for you with the correct base interface names.
For example, if you add IList<int> to a class's interface list, then
click the little box on the interface name and tell it to explicitly
implement the interface, it'll write some methods for IList<int>, some
for ICollection<int>, one for IEnumerable<int>, and one for
IEnumerable.

Jesse

Dec 16 '05 #3

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

Similar topics

8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
5
by: Vivek | last post by:
Hi, I am trying to get as much information on void pointers. How do we take help of void pointers in writing generic functions. I have seen this kind of code for many years, but now I have got...
2
by: Samuel R. Neff | last post by:
What options are available for doing full-text searches of database data without using a database-specific full-text engine? The only option I've found is Google's Search Appliance but it's an...
1
by: HC | last post by:
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...
9
by: Jon Davis | last post by:
I am working with an application that is compiled as a COM EXE (written in Delphi 7). I don't know if it's the nature of COM EXE or if it's implemented in the COM EXE, but when I create a new...
10
by: Brett Romero | last post by:
I'd like to design a class that is runtime typed (generic) and the types will always have a specific member. For example: public T MyClass { T temp = default(T); Void Fill {...
11
by: Don | last post by:
When using Visual Basic .NET with a reference to Interop.Outlook, is there a way to get more detailed information about an error other than Exception.Message or Exception.ToString? For example,...
12
by: Bob Jones | last post by:
I have an odd business requirement and I think that the implementation is not correct in the terms of OOP development. Any help on the concepts would be very appreciated! We currently have a...
6
by: Dan Smithers | last post by:
I want to write my own class derived from the ostream class. I have been getting errors with my templates: First, I get an error writing a nested template. If I leave the function definition...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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...
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,...

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.