I am currently building a measurement unit convertion app for Windows Phone. After setting up my user interface I added data bindings to two textboxes, each display a measurement. (e.g. Fahrenheit <-> celsius)
So far, so good. However, when I deploy my app I run into a cyclic dependency. Whenever I update one value, the conversion function is called and the other unit is update. Hence, its value is set causing the conversion function to run again.
What can I do to avoid this dependancy? I guess its more of a conceptual thing than actual programming. Thanks
Example code
private float _fahrenheit;
private float _celsius;
public float Fahrenheit {
get { return _fahrenheit; }
set {
_fahrenheit = value;
FahrenheitToCelsius();
}
}
public float Celsius
{
get { return _celsius; }
set
{
_celsius = value;
CelsiusToFahrenheit();
}
}
private void CelsiusToFahrenheit()
{
Fahrenheit = _celsius * 1.8f + 32.0f;
}
private void FahrenheitToCelsius()
{
Celsius = ((_fahrenheit - 32.0f) * 5.0f) / 9.0f;
}
I would pick one of them to be your base. The other one will always be a conversion of this. We do this with metric to imperial conversion for our app. All values are stored in metric but you can display either metric or imperial. If you were going to use Celsius as your base it would look something like this: