Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call one constructor from another

Tags:

c#

constructor

I have two constructors which feed values to readonly fields.

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; } 

One constructor receives the values directly, and the other does some calculation and obtains the values, then sets the fields.

Now here's the catch:

  1. 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.
  2. 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.
  3. I don't know how to call one constructor from another.

Any ideas?

like image 779
Avi Avatar asked Oct 24 '10 16:10

Avi


People also ask

How do you call one constructor from another?

Constructor chaining can be done in two ways: Within same class: It can be done using this() keyword for constructors in the same class. From base class: by using super() keyword to call the constructor from the base class.

Can we call constructor from another constructor?

The invocation of one constructor from another constructor within the same class or different class is known as constructor chaining in Java. If we have to call a constructor within the same class, we use 'this' keyword and if we want to call it from another class we use the 'super' keyword.

Can you call one constructor from another provide example?

Example 1: Java program to call one constructor from another Here, you have created two constructors inside the Main class. Inside the first constructor, we have used this keyword to call the second constructor. this(5, 2); Here, the second constructor is called from the first constructor by passing arguments 5 and 2.

How do you call a constructor from another in Java?

To call one constructor from another constructor is called constructor chaining in java. This process can be implemented in two ways: Using this() keyword to call the current class constructor within the “same class”. Using super() keyword to call the superclass constructor from the “base class”.


2 Answers

Like this:

public Sample(string str) : this(int.Parse(str)) { } 
like image 154
SLaks Avatar answered Oct 23 '22 07:10

SLaks


If what you want can't be achieved satisfactorily without having the initialization in its own method (e.g. because you want to do too much before the initialization code, 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 at will.

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); } 
like image 43
supercat Avatar answered Oct 23 '22 09:10

supercat