I need a place in my code where variables auto-update when other variables change that they are dependent on.
The difference from spreadsheets is I don’t want all the parsing stuff, I just want everything done in code as I don’t need to update formulas/variables after the program has been compiled.
Basically there is input data, and a few “cells” from the spreadsheet part will listen in on those values, change, and the change will propagate through the “spreadsheet” part. Like a normal spreadsheet.
So I can then immediately after depend on all those values, instead of having to write all kinds of code to update all the variables correctly first, which would be very hard to do.
If anyone can help me with the thought process to implement this it would really help! I’ve been reading some spreadsheet source code but it will take a long time to understand that, and then understand how I would change those ideas to fit what I need to do.
Edit:
Right now I just have things in loops and structure everything correctly so it updates correctly like so:
A1 = 2;
B1 = A1 + 2;
Then this just loops again and again. But if I wanted things to update automatically, how would I store the calculations attached to the variables? So when updating B it would call A1 + 2 ?
By the way A1, and B1 are just random variables. I don’t name things by cell names and there is no such structure.
One way of storing calculations is to use the elements of functional programming that have been built into C#.
A formula can be stored as a function, using one of the
Func<T1,T2,...TResult>delegates.Here,
CalculationForXspecifies a function that takes two doubles, and returns a the product of the two doubles. Then using for exampleCalculationForX(5, 2)returns 10.So, a straightforward way to keep track of all the dependencies would be to store an object for each dependent variable, containing its formula and inputs. These could be in the form of:
PropertyInfoarrayAction<T1,T2>delegate represents the actual property setterWhenever you receive a property changed event, retrieve the calculation and the inputs for all variables that depend on it, and update the target values.
(It’s funny to me that you asked this question, as I asked a quite similar one yesterday on Programmers.)