Calling a constructor from another constructor in the same class

15,877

Solution 1

In this case just do not use constructor calls, but something like :

public class FooBar()
{
    public FooBar(string s)
    {
        Init1();
    }

    public FooBar(int i)
    {
        Init2(); 
        Init1();
    }

}

Where I supposed that Init1(..) and Init2(..) are methods related to some specific intialization logic of corresponding constructor.

Actually you can arrange this function calls in a way that better fits your needs.

Solution 2

You can use another private method to do the initialization:

public class FooBar()
{
    public FooBar(string s)
    {
        this.Initialize(s);
    }

    public FooBar(int i)
    {
        this.Initialize("My String");
    }

    private void Initialize(string s) {
        // Do what constructor 1 did
    }
}

Solution 3

In general you could use this:

public class FooBar()
{
    private Test(string s)
    {
        // Some functionality
    }

    public FooBar(string s)
    {
        Test(s);
    }

    public FooBar(int i)
    {
        // Do what you need here
        Test("MyString");
    }    
}

Solution 4

Why not wrap that functionality into some method and call that in the constuctors (or wherever you want) ?

public class FooBar()
{
    public FooBar(string s)
    {
        LoadUser(s);
    }

    public FooBar(int i) : this("My String")
    {
        var s=CheckUser();
        LoadUser(s);
    }

    private string CheckUser()
    {
       return "the id/name from db";
    }
    private void LoadUser(string s)
    {
          //load the user
    }

}

This is a generic solution. You may change the return type according to your specific scenario.

Solution 5

Best approach - don't put that logic in the constructor. Break it out to other methods that you can call at will without worrying about constructor chaining.

Share:
15,877
Farhan
Author by

Farhan

Trying to learn what I have not learnt. SOreadytohelp

Updated on July 29, 2022

Comments

  • Farhan
    Farhan almost 2 years

    I have a class with two constructors (C#). Here is the code snippet:

    public class FooBar()
    {
        public FooBar(string s)
        {
            // constructor 1, some functionality
        }
    
        public FooBar(int i) : this("My String")
        {
            // constructor 2, some other functionality
        }
    
    }
    

    Yes, I know that I can call one constructor from another using the above mentioned approach. But in this scenario, if I call constructor 2, all statements in constructor 1 will run BEFORE the very statement in constructor 2 is executed.

    What I want is that after all statements in constructor 2 are run, then it will call constructor 1.

    In my exact situation, I am doing user authentication. Constructor 1 retrieves user information with just the user ID, but constructor 2 does the user authentication using email and password. If a user is in the database, it gets the user ID and now I want constructor 1 to populate all properties of the class.

    Please let me know if you require additional information. If you think that there is another better approach, I would be happy to listen the suggestion.

    UPDATE 1: I wonder why something like this is not implemented:

    public FooBar(bool b)
    {
        // constructor 3, some more functionality
        this.FooBar("My String"); // calling constructor 1
    }