Ok, so I've already solved this but I want to ask/share what you guys think.
I have a base class with 2 overloads for a method, for instance
public class Entity{
public virtual bool Save() {
//ReallySave()...
}
public virtual int Save(string someParam){
//also ReallySave()...
}
}
and I have a derived class, in which I would like to override whatever save() method I feel like overriding, for instance
class Person {
override int Save(string someParam){
console.log(someParam)
base.Save(someParam);
}
}
Now the problem is, somewhere else, I wan to call
person.Save();
or
person.Save("hello");
but I have no way to know (nor should I) which overload is overriden. This code falls due to its combination of overridable overloaded methods, since calling save() would not go into the overriden method.
So I did some trickery to make sure, at the base level, each base method calls the other, something like the "chain of responsability" pattern.
An example of the modifyed base class would be
public class Entity{
private bool WithParamsMethodCalled = false;
private bool NoParamsMethodCalled = false;
public virtual bool Save() {
NoParamsMethodCalled = true;
if(!WithParamsMethodCalled ) { Save(""); }
//ReallySave()...
}
public virtual int Save(string someParam){
WithParamsMethodCalled = true;
if(!NoParamsMethodCalled ) { Save(); }
///no need to call ReallySave() here
}
}
Now the question: Is there a better way to do this? The code works but
a. its smelly and
b. If i want more than 2 overloads it gets uglier
thanks!
Since I have many overridable methods all with the same name, I created a small pattern to keep track of which override was used (in base.MyMethod(). Then, the user can call any of the overloads and the app will cycle through all overloads until it sees there's one who set its "executed" flag to true. Then execute the "real base method" and break the loop. I guess this pattern could be described as "chained overloads for single override with indistinct entry point"