views:

194

answers:

5

I am getting this error:

type Bar does not implement interface IFoo

From this code:

public interface IFoo
{
   void DoA();

   void DoB();
}

public class Foo:IFoo
{

    void IFoo.DoA()
    {

    }

    void IFoo.DoB()
    {


   }
}

public class Bar:Foo
{

     void IFoo.DoA()
      {
          base.doA();

      }

      void IFoo.DoB()
      {
          base.doB();

      }
  }

I am using C# 2.0.

What am I doing wrong?

+3  A: 
public class Bar : Foo, IFoo
{ 
    // Your code
}
alex2k8
Why is IFoo required? why -1?
I guess -1 because people consider it to be obvious... By adding IFoo, you say that Bar has its own implementation, different from Foo's implementation. That's the point of it being explicit. Foo's explicit implentation stays with Foo.
Ruslan
Ruslan right, but -1 is not mine ;-)
alex2k8
+2  A: 

See n8wrl's answer as to how you should be doing this. See below for the reason why.

You can't explicity implement interface members (void IFoo.DoA()) when implicitly implementing the interface. In other words, because Bar only implements IFoo by virtue of extending Foo, you cannot use explicit implementation.

This will work:

public class Bar : Foo
{
    public void DoA()
    {
        ...
    }

    public void DoB()
    {
        ...
    }

Or this:

public class Bar : Foo, IFoo
{
    void IFoo.DoA()
    {
        ...
    }

    void IFoo.DoB()
    {
        ...
    }

A bigger problem that you'll probably face is that since Foo is not abstract, it must implement DoA and DoB. If you also implement these methods in bar, you will not be doing it polymorphically. You'll be hiding the Foo implementations if the code has a handle to the Bar type.

Michael Meadows
base.doA(); gives an error :)
right, that's why I commented it out. It's a separate issue. First, there's no implementation for doA in the base, and second, if you meant DoA, since it's explicitly implemented, it's not visible unless you cast base to IFoo...
Michael Meadows
Efen if you cast base to IFoo it's not visible because the compiler can't determine which IFoo to use (since both classes implement IFoo, and as I stated in the answer, it's not polymorphic.
Michael Meadows
+2  A: 

I have run into this as well. what is 'worse', depending on how you look at it is you can't define the interface implementations to be virtual to be overridden in descendent classes. I have gotten into the habit of doing this:

public class Foo:IFoo
{    
     void IFoo.DoA()
     {
          DoACore();
     }
     void IFoo.DoB()
     {
          DoBCore();
     }
     protected virtual void DoACore()
     {
     }
     protected virtual void DoBCore()
     {
     }
}
public class Bar:Foo
{     protected override void DoACore()
      {
          base.DoACore();
      }
      protected override void DoBCore()
      {
          base.DoBCore();
      }
}
n8wrl
Very good, close to what I want... where is Jon skeet?
+1, you should accept this answer. It's the "standard" practice in .Net for handling interface implementation in deep inheritance trees.
Michael Meadows
Definitely... I like this solution.... Michael are you t he same person as n8wrl, try to get move votes:)?
What is the naming convention for overriden methods?
Depends on where you're working. In WinForms, it's On[method], but it gets ugly, since On[Method] can be used for events as well. Just pick a convention that works for you and be consistent.
Michael Meadows
So why not simply use implicit implementation? Declare DoA, DoB virtual and you are done. Or do you have some goals in mind not mentioned here?
Ruslan
It certainly would work for implicit implementation. But I just find myself usually implementing explictly and overriding internal methods. Another reason: It is difficult for explicitly-implemented interface methods to call one another without funky casts. Anyone can call a *Core()
n8wrl
+1  A: 

I would suspect the confusion arises over how interfaces were implemented in C++, as abstract classes and multiple inheritance.

In .NET, and interface is simply a contract that says you will implement those methods.

You're code won't compile for the same reason this code wont compile(it would with C++):

public interface IFoo
{
    void DoSomething();
}

public abstract class Foo : IFoo
{
}

Foo is declaring that Foo implements IFoo, it doesn't say anything else about anyone.

If you wanted to force derived classes to implement it you would do:

public interface IFoo
{
    void DoSomething();
}

public abstract class Foo : IFoo
{
    public abstract void DoSomething();
}

Or if you really wanted the interface methods hidden by explicit implementation, then something like n8wrl posted above.

Darren Clark
sort of irrelevant, but thanks for your time anyway!
Sorry you think so... I thought the issue you were having was that in .NET interface methods are not automatically virtual as they are in C++.
Darren Clark
+1  A: 

At first, this is not clear what is the task? If the goals was to fix a problem asap, than 'Bar : Foo, IFoo' is the shortest solution.

If you are just trying to learn 'implicit/explicit' interfaces difference, than check this post.

Also notice, that 'n8wrl' solution is arguable. It is ok only if Foo indeed required to implement IFoo explicitly. If no, than there is a simpler way:

public interface IFoo
{
    void DoA();
}

public class Foo : IFoo
{
    public virtual void DoA() {}
}

public class Bar : Foo
{
    public override void DoA() {}
}

Hope this helps.

alex2k8
@Sasha, I just noticed that the post suggested is your question :-)
alex2k8
@alex2k8, the n8wrl's solution indeed work for me. thx