In article <uS**************@TK2MSFTNGP02.phx.gbl>,
ma********************@mvps.org (Mattias Sjögren) writes:
Regarding your second queston. Why not just implement IEllipse
directly in the Circle class? Or if you don't control the Circle
class, write a simple adapter class implementing IEllipse and
delegating to ICircle.
Ok, I'll giv another example:
Suppose this pattern:
struct DateValuePair {
Date date;
double value;
}
public interface IMyValues {
public double getValue(Date date);
}
public class MyValues: IMyValues {
private InterpolationMethod method;
private DateValuePair[] points;
public double getValue(Date date) {...}
}
Normally, you'd call it this way:
value = x.getValue(date);
However, assume that we from time to time have a simlple case where the
"MyValues" is just represented as double, i.e. the date-value structure
is "flat", e.g. a flat yield curve of interest rates.
In that case, we would like the double to behave as an IMyValue, i.e.
to return itself when the IMyValues.getValue is invoked on it, i.e.
x.getValue(date) returns x when x is a double.
Now when I'm writing this, though, I realize that this might also
work if there is an implicit cast from double to an IMyValues, e.g.
public class DoubleMyValues: IMyValues {
private double value;
public DoubleMyValues(double value) {this.value = value;}
public static implicit operator DoubleMyValues(double value) {
return new DoubleMyValues(value);
}
public double getValue(Date date) {return this.value;}
}
I'm relatively new to this kind oc casting in C#, so I'm not sure
it would work in all situations. In the test program i wrote i did
NOT work, there was no conversion from double to DoubleMyValues.
(Isn't implicit casting supposed to do just that??)
/Samuel