Hello,

Need a litle help with a problem.

It is posible to automatically change the value of variables? For example: If i have 2 variables: "a" and "b", and let's say initially "a"= 3 and "b"= "a"+5;

If i change "a" = 7, how can i do that "b" automatically change its value according with "a"???

Recommended Answers

All 7 Replies

For built in types you can not do this.

I suppose that you could create a class that referenced another value and used that as a basis to calculated the "value" of the class. However that would probably be very confusing to anyone reading your code and rather hard to maintain.

I think this is a question of should you do this rather than can you do this (which often occurs with C++) and the answer, in my opinion, is no you should not.

If you find you are often updating the value of b with a then perhaps the correct question is rather "is the design right?"

Could you find a better place to update the value of b or if it follows the value of a so closely could you eliminate it altogether?

Actually, what i want to do is something similar with the work in Excel. If i change a value in a cell, all the celles related with the first one will change their values automatically.

So, i think this is possible, but which is the easiest way?

What about

{
 a=new_value;
 b=a+x;
}

:/

You are funny Buffalo101, but the problem is much more complex than i presented here! how can u handle if you have hundred variables which depend on "a" in different ways? you write (call) again all that functiuns (to calculate all "b"-s )?

You are funny Buffalo101, but the problem is much more complex than i presented here! how can u handle if you have hundred variables which depend on "a" in different ways? you write (call) again all that functiuns (to calculate all "b"-s )?

Well, yes. You need to create a procedure that recalculates all your values. If we take a look at excel, that is what happens whenever you type a new function to a cell or change the value of a cell. Whenever you press enter or that green tick-button it signals that you are done editing, and the recalculate procedure is called. Pretty simple actually :)

Good luck to you!
Emil Olofsson

oh no, i don't think so! and if you have also variables (cells) that not depend on "a" ? what are u doing? recalculate for all cells? or verify which cell depend on "a" and for those u recalculate? this can be very time consuming for hundred or thousand of cells!

If you have ever used Excel with formulas in 1000s of cells then you would know that recalculation is very time consuming. In fact I had a sheet that had a few 100,000 cells filled in and I found that the best solution was to switch off automatic calculation and do a manual recalculate when I had finished all edits.

My point is recalculating everything is a brute force method but is quite common. You could try having each cell knowing what cells it is dependent on and only recalculating cells effected by the original change and any further changes that produces but you have to be careful, it is easy to get into an infinite loop that way if you have a circular dependency of cells.

You might try looking up the Observer Design pattern which might be of assistance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.