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
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++)....