Calling a constructor from another constructor in the same class

岁酱吖の 提交于 2019-12-23 08:38:53

问题


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
}

回答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.




回答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
    }
}



回答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");
    }    
}



回答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.




回答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.



来源:https://stackoverflow.com/questions/11122908/calling-a-constructor-from-another-constructor-in-the-same-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!