25
votes

How can I call base class' constructor after I've called my own constructor?

The problem is, base class' constructor calls an abstract method (overridden in sub class), which needs to access variable x, initialized in sub class' constructor?

Short example code:

abstract class BaseClass
{
    protected string my_string;

    protected abstract void DoStuff();

    public BaseClass()
    {
        this.DoStuff();
    }
}

class SubClass : BaseClass
{
    private TextBox tb;

    public SubClass()
        : base()
    {
        this.my_string = "Short sentence.";
    }

    protected override void DoStuff()
    {
        // This gets called from base class' constructor
        // before sub class' constructor inits my_string
        tb.Text = this.my_string;
    }
}

Edit: Based on answers, it obviously is not possible. Is there an automated way to call this.DoStuff(); on every object of SubClass once they're created? Of course I could just add this.DoStuff(); after all the other lines in sub class' constructor, but having around 100 of these classes, it feels stupid. Any other solution, or should I use the manual one?

4
Actually I don't think you can, at least not in a standart way (using reflection perhaps you could but I don't think it's a good approach)ppetrov
You can't. Think about it - your class is reliant on it's parent class. How can you create an instance of your class without creating the parent first?Daniel Kelley
See my edit. I guess I'll just manually add one line in every sub class' constructor, not that big of a deal. Still worth asking :-)user1632861

4 Answers

30
votes

You can't.

Also, you generally shouldn't call virtual methods in a constructor. See this answer.


Depending on your actual code and not the simple example you wrote, you could pass values as parameters of the base constructor and the DoStuff method. For example:

abstract class BaseClass
{
    private string my_string;

    protected abstract void DoStuff(string myString);

    public BaseClass(string myString)
    {
        this.my_string = myString;
        this.DoStuff(this.my_string);
    }
}

class SubClass : BaseClass
{
    private TextBox tb;

    public SubClass()
        : base("Short sentence.")
    {
    }

    protected override void DoStuff(string myString)
    {
        tb.Text = myString;
    }
}

If it's not possible with your actual code, then writing multiple DoStuff() will do the job. Also remember to seal your SubClass class so nobody else will be able to introduce bugs by modifying the DoStuff method another time.

5
votes

I would not call an abstract method in the constructor as it can happen that the constructor for the instance that invokes the method has not executed

When a virtual method is called, the actual type that executes the method is not selected until run time. When a constructor calls a virtual method, it is possible that the constructor for the instance that invokes the method has not executed. To fix a violation of this rule, do not call a type's virtual methods from within the type's constructors.

http://msdn.microsoft.com/en-us/library/ms182331%28v=vs.80%29.aspx

1
votes

You can't do it like that. But you can put your initiation code of the master class into a separate initialize method. You then can call that method in your specific constructor >after the constructor code.

0
votes

Is there a reason you can't construct a class before its constructor actually runs? It requires a greater level of responsibility on how the derived class is created and probably not a best practice, but it appears to work.

abstract class BaseClass {
    protected string my_string;

    protected abstract void DoStuff();

    public BaseClass() {
        this.DoStuff();
    }
}

class SubClass: BaseClass {
    TextBox tb;

    bool Constructed = false;
    void Constructor() {
        if (!Constructed) {
            Constructed = true;
            this.my_string = "Short sentence.";
        }
    }
    public SubClass()
        : base() {
        Constructor();
    }

    protected override void DoStuff() {
        Constructor();
        // This gets called from base class' constructor
        // before sub class' constructor inits my_string
        tb.Text = this.my_string;
    }
}