471,627 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 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 1571
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by JustSomeGuy | last post: by
5 posts views Thread by Vivek | last post: by
10 posts views Thread by Brett Romero | last post: by
6 posts views Thread by Dan Smithers | last post: by

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.