I have two properties in a class that update each other: binary
and value
, where binary
is the binary value and value
is the base 10 value. Each updates each other such that when the set
method of value
is called, binary
is updated, and vice versa.
private UInt64 _Binary;
public UInt64 Binary {
get { return _Binary; }
set
{
if (_Binary != value) {
_Binary = value;
BinaryToValue();
UpdateUi();
}
}
}
private double _Value;
public double Value {
get { return _Value; }
set
{
if (_Value != value) {
_Value = value;
ValueToBinary();
}
}
}
private void ValueToBinary() {
// Loop through and set each bit high or low as needed
}
If the current Value
is a decimal 1
and just the LSB of Binary
is set high, then if I change the value to 6
, now I am setting 3 bits to different values, meaning the set
method of Binary
is seeing a different value each time (set bit 0 low (value = 0), set bit 1 high (value = 2), set bit 2 high (value = 4)). In this scenario, BinaryToValue()
is also called three times, so the Value
property is changing when each of the the three bits is set to a different state, which it should not be because the Value
property initially contained the correct value.
How do I prevent this from occurring?
I can't update the backing field because I need the UI to be updated when Binary
is modified. I took a look at this question: Properties that update each other, but the solution there does not apply here because I can't update the backing field only.