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);
}
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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)
{
}
}
Method 2
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;
}
}
Method 3
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();
}
}
Method 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();
}
}
Method 5
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){}
Method 6
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();
}
}
Method 7
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?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0