I have two constructors which feed values to readonly fields.
class Sample
{
public Sample(string theIntAsString)
{
int i = int.Parse(theIntAsString);
_intField = i;
}
public Sample(int theInt)
{
_intField = theInt;
}
public int IntProperty
{
get { return _intField; }
}
private readonly int _intField;
}
One constructor receives the values directly, the other does some calculation and obtains the values, then sets the fields.
Now here's the catch:
- I don't want to duplicate the setting code. In this case, just one field is set but of course there may well be more than one.
- To make the fields readonly, I need to set them from the constructor, so I can't "extract" the shared code to a utility function.
- I don't know how to call one constructor from another.
Any ideas?