Suppose I have the following base and derived class in my C# program:
class BaseClass
{
public BaseClass() { }
public BaseClass(int someVal)
{
// do something here
}
}
class DerivedClass : BaseClass
{
public DerivedClass(string somethingNew)
: base()
{
// do something with the somethingNew varible
}
public DerivedClass(string somethingNew, int someVal)
: base(someVal)
{
// do **THE SAME THING** with the somethingNew varible as in first constructor
}
}
My question comes about in the final constructor - Ideally, what I'd like is to be able to do something along the lines of (although this won't compile):
public DerivedClass(string somethingNew, int someVal)
: base(someVal)
: this(somethingNew)
{
}
In other words, call both the base and the first constructor.
Is there any way to do this or do I just need to re-write the code inside each constructor in this case?
Thanks!!