Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i access the previous values of a variable?
consider i have initialized the x value like this:
x = 5;
then i set the x = 10;
and at the third line i set the x = 20;
how can i access the previous values of x ?
Posted
Comments
BillWoodruff 17-Nov-15 10:24am    
Since you cannot overload the assignment operator in C#, no you can't do this "directly."

However, it is relatively easy to create a generic Class that will maintain a list of previous values for you, and define 'Set and 'Get operators to use with it which will always update a 'CurrentValue field for the Class instance. If you want to see an example of this, just ask me.

You cannot, unless you have stored these previous values elsewhere.
A variable holds a single value, and does not maintain a history of the different values is has held.
 
Share this answer
 
Comments
Patrice T 17-Nov-15 6:48am    
+5
It doesn't exist natively in the language C++ or C#.

To do what you want, the only way is to manually build a system that memorize your variable values.

The only practical usage I see is as a part of a debugger that would allow backward execution.
 
Share this answer
 
You cannot: memory is overwritten, previous values are lost.
If you need the previous values then you have to store them explicitely (e.g. using a collection, like for instance, a std::vector, or a plain array).
You may nicely wrap such a mechanism in your own 'Variable' class.
 
Share this answer
 
Comments
Patrice T 17-Nov-15 6:49am    
+5
CPallini 17-Nov-15 10:50am    
Thank you.
You can't.
Integers don't have a "history" that you can access, it's not like writing things down on paper and "crossing them out" when you have fished with them. It's more like taking a jar and putting five coins in. If you then empty it, mix all your coins together and put ten random coins in the jar you can't tell how many the jar had before.

Variables do not normally have a "history", except in very special cases when an audit trail is required, and then a lot of software goes on behind the scenes to log when changes are made, and by whom.
 
Share this answer
 
Comments
Patrice T 17-Nov-15 6:49am    
+5
This is not possible unless you store the variable history yourself.
Variables is a simple storage location in memory not a DB with audit trails.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900