28

I have the following hierarchy:

class Base
{
  public Base(string sMessage)
  {
     //Do stuff
  }
}

class Derived : Base
{
  public Derived(string someParams)
  {

   string sMessage = "Blah " + someParams;

   //Here I want to call the base constructor
   //base(sMessage);

  }

}
0

7 Answers 7

21

You have to call the base class constructor prior to the derived class constructor's body.

class Derived : Base
{
  public Derived(string someParams)
    : base("Blah " + someParams)
  {

  }

}
Sign up to request clarification or add additional context in comments.

2 Comments

This is probably the best solution for this simple case, but would get ugly pretty quickly if constructing the parameter was much more complex.
That's true, though you could also construct the parameter in a static method and pass the result to the base class constructor. I prefer that over the hook approach. Or, since this is .NET, you could remove the things from the constructor and do it in the setters, which is serialization-friendly.
12

I originally missed OregonGhost's comment about using a static method to modify the parameter, which turned out to be the most useful for me, so I thought I'd add a code sample for others who read this thread:

class Base
{
    public Base( string sMessage )
    {
        // Do stuff
    }
}

class Derived : Base
{
    public Derived( string sMessage ) : base( AdjustParams( sMessage ) )
    {
    }

    static string AdjustParams( string sMessage )
    {
        return "Blah " + sMessage;
    }
}

Comments

9

You can't. You can call it before:

public Derived() : base()

or you have to use a hook

class Base
{
  protected void init() { }
  public Base(string sMessage)
  {
     init();
  }
}

class Derived : Base
{
  public Derived(string someParams)
  {
   string sMessage = "Blah " + someParams;
   init();
  }
}

4 Comments

Since the Base constructor is supposed to do stuff (presumable some stuff with sMessage), how does this code solve his problem?
If you use the hook, you need to have a way to avoid calling the default base constructor. See my answer for a way to avoid this by providing a constructor that doesn't run the initializer.
@wcm: I'm just showing the steps to achieve the order of operations that DotnetDude needs. It looks like he understands C# well enough to alter the params and return object to fit his actual needs from here.
@Dinah -- without a default constructor, this won't even compile. If you add a default constructor that calls init() then init() gets invoked twice: once in the base constructor and once in the derived constructor. If you implement with a hook you need to have a constructor that doesn't run it.
4

If you really need to have your constructor run first, then I suggest using a protected Initialize method that is invoked by your constructors and does the actual work of initializing the class. You need to provide an alternate constructor that will allow the initialization to be skipped.

public class Base
{

    public Base() : this(true) { }

    protected Base(bool runInitializer)
    {
        if (runInitializer)
        {
            this.Initialize();
        }
    }

    protected void Initialize()
    {
        ...initialize...
    }
}

public class Derived : Base
{
    // explicitly referencing the base constructor keeps
    // the default one from being invoked.
    public Derived() : base(false)
    {
       ...derived code
       this.Initialize();
    }
}

Comments

1

Points to be noted on constructors:

· Constructors cannot be "virtual".

· They cannot be inherited.

· Constructors are called in the order of inheritance.

public Child(string a):base(a){}

Comments

0
public Derived(string someParams) : base(someParams)
{
    string sMessage = "Blah " + someParams;
}

This is the way you have to do it. You could perhaps put the code you want to call afterwards in a protected method in the base class and then you could call it afterwards like this:

class Base
{
  public Base(string sMessage)
  {
     ConstructorStuff();
  }

  protected Base()
  {
  }

  protected void ConstructorStuff()
  {
  }
}

class Derived : Base
{
  public Derived(string someParams)
  {    
   string sMessage = "Blah " + someParams;

   ConstructorStuff();       
  }    
}

5 Comments

This will run ConstructorStuff twice -- once when the default base constructor is invoked before the derived constructor and once during the execution of the derived constructor.
Erm? Base doesn't have a default constructor that calls ConstructorStuff?
Ah. Then you need to provide a default constructor or the code won't compile since your derived class will need it. The problem still remains that the default constructor will need to use ConstructorStuff as well and you need an alternate that doesn't invoke the initializer.
That'll learn me for missing out the "too obvious to bother" code from a basic C# question. The altered code will do what he wants now won't it?
The problem now is that every derived class is required to call the initializer. What I think you want is to have the initialize run by default but have a way to prevent it if necessary. This will do the job, but isn't IMO the best solution.
0

Actually, the simplest solution is:

class Base
{
  public Base(string sMessage)
  {
     //Do stuff
  }
}

class Derived : Base
{
  public Derived(string someParams)
    : base("Blah " + someParams)
  {
  }

}

Why make it more complicated?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.