met.lord met.lord - 1 month ago 8
ASP.NET (C#) Question

Inheritance from generic class with abstract property

I'm trying to have a class inherit from an abstract class with a generic property in it. I think I'm missing something real important. The only way I can get rid of this error is getting rid of the constructor in the base class. But if I do that then my class would lack of purpose.

Error


Error CS7036 There is no argument given that corresponds to the
required formal parameter 'propA'...


Base Class

public abstract class BaseClass<T> where T:class
{
public string propA{ get; set; }
public int propB{ get; set; }
public IEnumerable<T> propC { get; set; }

public BaseClass(string propA, int propB, IEnumerable<T> propC)
{
this.propA = propA;
this.propB = spropB;
this.propC = propC;
}

public abstract IEnumerable<T> Method1();

public abstract string Method2();

public abstract void Method3();
}


Derived Class

public class DerivedClass: BaseClass<SomeClass>
{
public override IEnumerable<SomeClass> Method1()
{
//Code Here
}

public override string GetDefaultSortField()
{
//Code Here
}

public override void SetSortParams()
{
//Code Here
}
}

Answer

You do not have an explicit constructor in your derived class, so it 'inherits' the constructor of the base class. But the base class constructor has a parameter that uses a generic type T, which is not defined in the derived class. If you add a constructor explicitly to the derived class, it should be fine. I mean like this:

    public DerivedClass(string propA, int propB, IEnumerable<SomeClass> propC)
        : base(propA, propB, propC)
    {
    } 
Comments