Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call constructor inside the class?

Tags:

c#

I want to call constructor inside the class like: public class Myclass(){

   public MyClass(){
      //......
   }

   public MyClass(int id):this(){
      //......
   }

   private void Reset(){
      //.....
      this = new MyClass(id);    //here I want to call constructor
      //......
   }
}

but it is not working. Is it possible and how can I do it if Yes?

like image 411
KentZhou Avatar asked Jan 04 '12 16:01

KentZhou


People also ask

Can we call constructor inside constructor?

Yes, any number of constructors can be present in a class and they can be called by another constructor using this() [Please do not confuse this() constructor call with this keyword]. this() or this(args) should be the first line in the constructor.

How do you call a class constructor in C#?

To call one constructor from another within the same class (for the same object instance), C# uses a colon followed by the this keyword, followed by the parameter list on the callee constructor's declaration. In this case, the constructor that takes all three parameters calls the constructor that takes two parameters.

Can we call constructor one class to another class?

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 explicitly?

Explicit use of the this() or super() keywords allows you to call a non-default constructor. To call a non-args default constructor or an overloaded constructor from within the same class, use the this() keyword. To call a non-default superclass constructor from a subclass, use the super() keyword.


2 Answers

Simple answer: You can't.

Slightly more complicated answer: Move your initialization logic into a separate method that can be called from the constructor and your Reset() method:

public class MyClass
{
    public int? Id { get; }

    public MyClass()
    {
        Initialize();
    }

    public MyClass(int id)
    {
        Initialize(id);
    }

    public Reset()
    {
        Initialize();
    }

    private Initialize(int? id = null)
    {
        // initialize values here instead of the constructor
        Id = id;
    }
}
like image 115
Justin Niessner Avatar answered Oct 12 '22 15:10

Justin Niessner


You can't. But what you could do is split the constructor logic into an Initialize method that then reset could call.

   public MyClass(){
   //......
   }

   public MyClass(int id):this(){
      Initialize(id);
   }

   private void Initialize(int id){
   //....
   }

   private void Reset(){
      //.....
      Initialize(id);
      //......
   }
}
like image 35
Timothy Carter Avatar answered Oct 12 '22 15:10

Timothy Carter