0

I'm trying to implment a property interface with a class inherit the declare one. Maybe the example is more easy to understand.

MyClassA is OK, but MyClassB has a compile error 'Test.MyClassB' does not implement interface member 'Test.IMyInterface.PropA'. Any Idea how I can do this?

/****** EDITED CODE ******/

public class BaseClass
{
    public int PropA { get; set; }
}

public class InheritClass : BaseClass
{
    public int PropB { get; set; }
}

public interface IMyInterface
{
    BaseClass PropClass { get; set; }
}


public class MyClassA : IMyInterface
{

    public BaseClass PropClass { get; set; }
}

public class MyClassB : IMyInterface
{

    public InheritClass PropClass { get; set; }
}
3
  • 1
    MyClassB doesn't implement PropA. What are you trying to do? You're not inheriting MyClassB from anything. Commented Jun 18, 2015 at 9:02
  • FYI - you can right click on the interface a class is inheriting from and 'Implement' any methods / properties the interface describes. (In visual studio, other IDE's may vary) Commented Jun 18, 2015 at 9:15
  • 1
    Okay, right, this is a lot clearer. You must use the exact same signature. Even though InheritClass inherits from BaseClass, the method signature still has to use BaseClass. The actual value may be InheritClass, but not the signature. Maybe you'd want to use a generic interface instead? Or a helper property? Commented Jun 18, 2015 at 11:44

3 Answers 3

1

this is what you should do

public class BaseClass
{
    public int PropA { get; set; }
}

public class InheritClass : BaseClass
{
    public int PropB { get; set; }
}

public interface IMyInterface
{
    BaseClass PropClass { get; set; }
}


public class MyClassA : IMyInterface
{

    public BaseClass PropClass { get; set; }
}

public class MyClassB : IMyInterface
{
    private BaseClass _propClass;

    public BaseClass PropClass
    {
        get { return (InheritClass)_propClass; }
        set { _propClass = (InheritClass)value; }
    }
}

this is not directly possible, also why you are trying to do this. if you have base class you can use its child to set or get...

Sign up to request clarification or add additional context in comments.

2 Comments

this is correct. but i believe you are trying to achieve something that is not clear. the problem is somewhere else i guess @DiegoMartelli
Unfortunately it was clear, I only copy the wrong code. Thank you @M.kazem Akhgary.
0

Your interface requires that all classes that implement it have the property int PropA { get; set; }, but your class MyClassB doesn't have this method yet is inheriting IMyInterface.

So you have two options.

One, just implement the interface:

public class MyClassB : IMyInterface
{
    public int PropA { get; set; }
    public int PropB { get; set; }
}

Or, two, if you wish to only expose PropB on MyClassB then you could explicitly implement the interface (and assuming that PropB should be the implementation) then you would do this:

public class MyClassB : IMyInterface
{
    int IMyInterface.PropA { get { return this.PropB; } set { this.PropB = value; } }
    public int PropB { get; set; }
}

Comments

0

You should add PropA in implements of MyClassB:

public class MyClassB : IMyInterface
{
**public int PropA { get; set; }**
public int PropB { get; set; }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.