Search code examples
c#derived-class

Error implementing an abstract method


I'm trying to define a base class like this

public abstract class EntityBase
 {
 // some other things here
     public abstract BindingList<EntityBase> BindingList();
 }

When I define a derived class

public class SomeEntity: EntityBase
 {
     public BindingList<SomeEntity> BindingList()
     {
         BindingList<SomeEntity> result = new BindingList<SomeEntity>();
         // code to populate the list
         return result;
     }
  }

the compiler says:

'AbstractDataContext.SomeEntity' does not implement inherited abstract member 'AbstractDataContext.EntityBase.BindingList()'

Obviously, the problem is that the compiler can not solve SomeEntity is EntityBase, and therefore (IMHO) should satisfy the abstract definition, so the method return does not match the definition in the base class. How can the derived class satisfy this requirement?


Solution

  • Your function needs to return BindingList<EntityBase> to satisfy the contract. Otherwise it is a different function signature. However, since SomeEntity is a decendant of EntityBase you can return a BindingList<SomeEntity> that is upcast to a BindingList<EntityBase>. But the signature of the function has to match.

    Also, you need the override keyword in the method signature of the SomeEntity class:

    public override BindingList<EntityBase> BindingList()