• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 828
  • Last Modified:

Calling parent class constructor at end of child class constructor?

Is it possible to make a class call its parent class' constructor at the END of its constructor, instead of the beginning?

For example, please see the code below.

I hope this makes sense...
public class ParentClass
{
  public ParentClass()
  {
    //Do parent stuff here
  }
}
 
public class ChildClass
{
  public ChildClass()
  {
    // 1. Do some child stuff here
    // 2. Call parent class constructor now
  }
}

Open in new window

0
JimR123b
Asked:
JimR123b
1 Solution
 
LordOfPortsCommented:
No, the base class is automatically instantiated before the derived class. You could have a custom parent class constructor that takes a parameter and does not have the code you wish to delay and call it from the default constructor of the child class, e.g.:

public class CParent
{
    public CParent()
    {
        // This default constructor has some code you wish to delay
    }

    public CParent(string sText)

    {
         // This constructor would not have the code the default constructor has
    }

    // Add a function that does what the default constructor would have done
}


// IN THE CHILD CLASS
public  CChild() : base("some value")
{
    // Here you do some work
    // Now call the parent function that does what the default constructor would have done
}



0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now