C# Call base class' constructor after own constructor?

44,338

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
44,338

Related videos on Youtube

Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    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?

    • ppetrov
      ppetrov over 11 years
      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)
    • Daniel Kelley
      Daniel Kelley over 11 years
      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?
    • Admin
      Admin over 11 years
      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 :-)