Cannot enforce base constructor of abstract class in derived class

I am trying to enforce a specific parameterized constructor in my derived classes as per the answer below:

Abstract class with constructor

Using the example provided in the above answer, the compilation of the code does not proceed as expected. Even after changing the code to be similar to mine, it still fails. My actual code, although it compiles, is just fine. I am here at a loss as to why this is so.

Here's the cited example from the provided answer (which won't compile as expected):

public interface IInterface
{
    void doSomething();
}


public interface IIInterface : IInterface
{
    void doSomethingMore();
}


public abstract class BaseClass : IIInterface
{
    public BaseClass(string value)
    {
        doSomethingMore();
    }

    public void doSomethingMore()
    {

    }

    public void doSomething()
    {

    }
}


public sealed class DerivedClass : BaseClass
{
    public DerivedClass(int value)
    {

    }

    public DerivedClass(int value, string value2)
        : this(value)
    {

    }
}

      

Now my code, which compiles without fail:

public interface IMethod
{
    Url GetMethod { get; }
    void SetMethod(Url method);
}


public interface IParameterizedMethod : IMethod
{
    ReadOnlyCollection<Parameter> Parameters { get; }
    void SetParameters(params Parameter[] parameters);
}


public abstract class ParameterizedMethod : IParameterizedMethod
{

    public ParameterizedMethod(params Parameter[] parameters)
    {
        SetParameters(parameters);
    }


    private Url _method;
    public Url GetMethod
    {
        get
        {
            return _method;
        }
    }

    public void SetMethod(Url method)
    {
        return _method;
    }


    public ReadOnlyCollection<Parameter> Parameters
    {
        get
        {
            return new ReadOnlyCollection<Parameter>(_parameters);
        }
    }

    private IList<Parameter> _parameters;

    public void SetParameters(params Parameter[] parameters)
    {

    }
}


public sealed class AddPackageMethod : ParameterizedMethod
{
    public AddPackageMethod(IList<Url> links)
    {

    }

    public AddPackageMethod(IList<Url> links, string relativeDestinationPath)
        : this(links)
    {

    }

    private void addDownloadPathParameter(string relativeDestinationPath)
    {

    }

    private string generatePackageName(string destination)
    {
        return null;
    }

    private string trimDestination(string destination)
    {
        return null;
    }

}

      

I've removed the implementation in some of the methods to make it as concise as possible. As a side note, my actual code may be missing in scopes. Consider those parts of the WIP.

Update 1 / Solution:

As per sstan's answer below, pointing out that the ramifications of using the "params" keyword here are a corrected pass of my code that makes it behave as intended (crashing on compilation):

public abstract class ParameterizedMethod : IParameterizedMethod
{
    public ParameterizedMethod(Parameter[] parameters) // **'params' removed**
    {
        SetParameters(parameters);
    }
     // original implementation above      
}

      

+3


source to share


1 answer


The next constructor tries to call the base class constructor without any parameters.

public AddPackageMethod(IList<Url> links)
{

}

      

Well, it just so happens that the constructor of your base class can be called without any parameters due to the keyword params

. So it compiles just fine.



public ParameterizedMethod(params Parameter[] parameters)
{
    SetParameters(parameters);
}

      

Just for testing purposes, if you remove the keyword params

, thereby forcing the passed parameter, your code will not compile as expected.

+4


source







All Articles