Coder Perfect

Invoke a constructor from another constructor.

Problem

I have two constructors that feed readonly fields with values.

public class Sample
{
    public Sample(string theIntAsString)
    {
        int i = int.Parse(theIntAsString);
        _intField = i;
    }

    public Sample(int theInt) => _intField = theInt;
    public int IntProperty    => _intField;

    private readonly int _intField;
}

The values are received directly by one constructor, while the other does some calculations and acquires the values before setting the fields.

But there’s a catch:

Any ideas?

Asked by Avi

Solution #1

Like this:

public Sample(string str) : this(int.Parse(str)) { }

Answered by SLaks

Solution #2

If you can’t get what you want without putting the initialization code in its own method (for example, because you want to do too much before it, or wrap it in a try-finally, or whatever), you can have any or all constructors pass the readonly variables by reference to an initialization routine, which will then be able to manipulate them as needed.

public class Sample
{
    private readonly int _intField;
    public int IntProperty => _intField; 

    private void setupStuff(ref int intField, int newValue) => intField = newValue;

    public Sample(string theIntAsString)
    {
        int i = int.Parse(theIntAsString);
        setupStuff(ref _intField,i);
    }

    public Sample(int theInt) => setupStuff(ref _intField, theInt);
}

Answered by supercat

Solution #3

Use one of the following before the constructor’s body:

: base (parameters)

: this (parameters)

Example:

public class People: User
{
   public People (int EmpID) : base (EmpID)
   {
      // Add more statements here.
   }
}

Answered by Sudantha

Solution #4

I’m trying to improve on supercat’s response. I suppose the following can be done as well:

class Sample
{
    private readonly int _intField;
    public int IntProperty
    {
        get { return _intField; }
    }

    void setupStuff(ref int intField, int newValue)
    {
        //Do some stuff here based upon the necessary initialized variables.
        intField = newValue;
    }

    public Sample(string theIntAsString, bool? doStuff = true)
    {
        //Initialization of some necessary variables.
        //==========================================
        int i = int.Parse(theIntAsString);
        // ................
        // .......................
        //==========================================

        if (!doStuff.HasValue || doStuff.Value == true)
           setupStuff(ref _intField,i);
    }

    public Sample(int theInt): this(theInt, false) //"false" param to avoid setupStuff() being called two times
    {
        setupStuff(ref _intField, theInt);
    }
}

Answered by Faisal Mq

Solution #5

Here’s an example of a constructor that calls another constructor and then checks the property it sets.

    public SomeClass(int i)
    {
        I = i;
    }

    public SomeClass(SomeOtherClass soc)
        : this(soc.J)
    {
        if (I==0)
        {
            I = DoSomethingHere();
        }
    }

Answered by pasx

Post is based on https://stackoverflow.com/questions/4009013/call-one-constructor-from-another