Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Constructor cannot call itself (calling private constructor with base class argument)

This is similar to the question asked here: Constructor cannot call itself c# In fact some people probably will point out that it is the same, but I can’t figure out what needs to be done because of the differences in implementation.

While compiling the code below, I am getting the error:

Constructor "…" cannot call itself

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class ImprovementWithProductionCost : IComparable
{
    public ImprovementWithProductionCost(StarSystemImprovement improvement, ICostResolutionContext costResolutionContext, ICostProvider costProvider)
        : this(improvement, costResolutionContext, costProvider)
    {
        this.ImprovementDefinitionName = improvement.StarSystemImprovementDefinition.Name;
    }

    public ImprovementWithProductionCost(PlanetImprovement improvement, ICostResolutionContext costResolutionContext, ICostProvider costProvider)
        : this(improvement, costResolutionContext, costProvider)
    {
        this.ImprovementDefinitionName = improvement.PlanetImprovementDefinition.Name;
    }

    private ImprovementWithProductionCost(Improvement improvement, ICostResolutionContext costResolutionContext, ICostProvider costProvider)
    {
        this.Improvement = improvement;
        for (int i = 0; i < costProvider.Costs.Length; i++)....

So, unlike of how it is in the similar question, I do need to call private constructor with the same amount of arguments as the public one. And the public one constains different class types for the improvement argument.

I seem to be lacking a bit of knowledge of how to refactor this properly.

>Solution :

The problem isn’t the number of arguments per se, it’s the fact that you’re literally telling the compiler to have the constructor for (StarSystemImprovement, ICostResolutionContext, ICostProvider) call the constructor for (StarSystemImprovement, ICostResolutionContext, ICostProvider) — the exact same constructor.

I assume StarSystemImprovement and PlanetImprovement each inherit from Improvement. To make the call go to the private constructor expecting an Improvement, cast it so that the type is just Improvement, not a subtype:

public class ImprovementWithProductionCost : IComparable
{
    public ImprovementWithProductionCost(StarSystemImprovement improvement, ICostResolutionContext costResolutionContext, ICostProvider costProvider) : this((Improvement)improvement, costResolutionContext, costProvider)
    // −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^
    {
        this.ImprovementDefinitionName = improvement.StarSystemImprovementDefinition.Name;
    }

    public ImprovementWithProductionCost(PlanetImprovement improvement, ICostResolutionContext costResolutionContext, ICostProvider costProvider) : this((Improvement)improvement, costResolutionContext, costProvider)
    // −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^
    {
        this.ImprovementDefinitionName = improvement.PlanetImprovementDefinition.Name;
    }

    private ImprovementWithProductionCost(Improvement improvement, ICostResolutionContext costResolutionContext, ICostProvider costProvider)
    {
        this.Improvement = improvement;
        for (int i = 0; i < costProvider.Costs.Length; i++)....
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading