Occasionally, I'm confronted with the problem that I have to provide some constructors in order to initialize objects with different sets of data. These sets can be mathematically transformed into each other, for example:
public Element(int x, int y, int width, int height)
{
setValues(width, height, x, y);
}
public Element(int RectTop, int RectLeft, int RectRight, int RectBottom)
{
setValues(RectRight - RectLeft, Math.Abs(RectBottom - RectTop), RectLeft, -RectTop);
}
With such an desired overloading, which actually isn't any because of the same signatures, it is probable that the wrong constructor is invoked, e.g. you provide the Rect-Values and the first constructor is invoked. Assuming, we would have different types, we could achieve it by swapping types with good knowledge that this is a bad practice. But that's simply not the case in this easy example with four times int type.
Can you think of some elegant alternatives to achieve the desired behavior that we have the convenience of overloading, but with the same signature?
Element(int, int, int, int)
where the ints might be top, left, bottom, right or top, left, width, height. – Dan Pichelman Jun 5 '13 at 17:41