I try to save code and reduce failure rate for maintainability by reusing constructors as much as possible. Now, I came across a Situation, where I think I'll have to duplicate code, but maybe you know a solution.
public class Bar {
public List<string> BarList {
get;
private set;
}
public Bar() {
this.BarList = new List<string>();
}
public Bar(XmlNode node)
: this() {
//create from xml
}
}
public class Foo: Bar {
public List<int> FooList {
get;
private set;
}
public Foo()
: base() {
this.FooList = new List<int>();
}
public Foo(XmlNode node)
: base(node) {
//create from enhanced xml
}
}
Each constructor with XMLNode as parameter calls the parameterless constructor before for initialisation. But how to manage, that the derived class Foo calls its own parameterless constructor AND the the constructor with the XmlNode parameter of the base class?
The desired behaviour of constructor chain would be:
Foo(XmlNode)->Bar(XmlNode)->Foo()->Bar()
base(node)should callbase()already if it's required.Foo<T>{ List<T> FooList}Each constructor with XMLNode as parameter calls the parameterless constructor before for initialisation- I don't seeFoo(XmlNode node)callingthis(). Isn't this kind of thing usually handled with an internal private (or protected).Init()method that can be used by all constructors?