Search code examples
c#constructoroverloadingcopy-constructordefault-constructor

Copy Constructor going to base constructor and overwriting copied values


Constructor Conundrum, I have these two constructors. One is for making a copy of the class and the other is the standard constructor. I need to call the first one so that I can use the rule in it. I shouldn't have to repeat code so I am looking for a way to keep from overwriting the two dimensions.

public SprinklerLineModel()
    {

        NearCrossMainDimension = new PipeDimensionModel();
        FarCrossMainDimension = new PipeDimensionModel();

        this.AddValidationRule(Rule.CreateRule(() => BranchLineDiameter, RuleMessage.GREATER_THAN_ZERO, () => BranchLineDiameter > 0));
    }

    /// <summary>
    /// Copy Constructor
    /// </summary>
    /// <param name="sprinklerLineModel">Original copy of sprinklerLineModel</param>
    public SprinklerLineModel(SprinklerLineModel sprinklerLineModel)
        :this()
    {
        this.EstimatedFlow = sprinklerLineModel.EstimatedFlow;
        this.EstimatedPressure = sprinklerLineModel.EstimatedPressure;
        this.NearCrossMainDimension = new PipeDimensionModel(sprinklerLineModel.NearCrossMainDimension);
        this.FarCrossMainDimension = new PipeDimensionModel(sprinklerLineModel.FarCrossMainDimension);
        this.BranchLineDiameter = sprinklerLineModel.BranchLineDiameter;
        this.LeadLinePipeFittingLength = sprinklerLineModel.LeadLinePipeFittingLength;
        this.ExbPipeFittingLength = sprinklerLineModel.ExbPipeFittingLength;

        this.IsDirty = sprinklerLineModel.IsDirty;
    }

I want to be able to call the default constructor with out overwriting the two variables. Any help would be appreciated.

I suppose I could check and see if they are null but that doesn't seem like it is the best way.

Thanks


Solution

  • I would extract an initialization method and call said method from both constructors.

    public SprinklerLineModel()
    {
        NearCrossMainDimension = new PipeDimensionModel();
        FarCrossMainDimension = new PipeDimensionModel();
        Init();
    }
    
    public SprinklerLineModel(SprinklerLineModel sprinklerLineModel)
    {
        this.EstimatedFlow = sprinklerLineModel.EstimatedFlow;
        this.EstimatedPressure = sprinklerLineModel.EstimatedPressure;
        this.NearCrossMainDimension = new PipeDimensionModel(sprinklerLineModel.NearCrossMainDimension);
        this.FarCrossMainDimension = new PipeDimensionModel(sprinklerLineModel.FarCrossMainDimension);
        this.BranchLineDiameter = sprinklerLineModel.BranchLineDiameter;
        this.LeadLinePipeFittingLength = sprinklerLineModel.LeadLinePipeFittingLength;
        this.ExbPipeFittingLength = sprinklerLineModel.ExbPipeFittingLength;
        this.IsDirty = sprinklerLineModel.IsDirty;
        Init();
    }
    
    void Init()
    {
        this.AddValidationRule(Rule.CreateRule(() => BranchLineDiameter, RuleMessage.GREATER_THAN_ZERO, () => BranchLineDiameter > 0));
    }