I have a data object with three fields, A, B and C. The problem is that the user can set any of them because:
A * B = C
So if a user starts off by setting A & B, C will be calculated. but then if a user set C, A gets recalculated, so there is an implicit anchoring that is happening based off the last field that the user set.
i want to avoid a solution with a lot of flag member variables. Any best practices on how i can code this class up without having a lot of stuff like this below
public class Object { private double _A; private double _B; private double _C; private bool _enteredA; private bool _enteredB; private bool _enteredC; public double A { get { if (_enteredC) { return _C / _B; } else { return _A; } } }
should the logic of setting A, B, and C always be on the ‘get’ or the ‘set’.
Is there any cleaner way to doing this?
So your rules are:
What you’d need to do is remember the last two fields that the user entered, and from that, calculate the third. Therefore if they entered A and then B, then you calculate C. If they then change C, then recalculate A, and so on.
If you want to code it without using boolean flags, there’s a couple of different ways you could do that. A queue, or just a simple array/map would work:
Example usage: