Click here to Skip to main content
15,894,630 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
I want to keep old values (rather I can say original value) and modified value of a variable in same property. the type of property may be int/string/decimal.

for example:
class test{

public int salary{get;set;}
public string month{get;set;}
}

when i use this
test t = new test();
//initializing
t.salary=10;
t.month="apr";
//modifying
t.salary=15;
t.month="mar";

if i do t.salary it will obviously give me last modified value i.e. 15, but i want 10 as well so that it will help me further to show difference.. last value and new value.

is there any way through c# Attributes or any design pattern to keep original value and new value.

I can do this by maintaining two object of test class(original and latest) but there might be lot of properties in a class and only few needs such feature thats why i don't want to maintain two object.

What I have tried:

I thought I can do this using c# attributes but don't know how to do so.
Posted
Updated 3-Jan-19 6:09am
Comments
[no name] 3-Jan-19 9:18am    
Have another property to maintain previous value

First, take a look at these CodeProject articles::

Automated Undo/Redo library based on C# Generics in .NET [^]

Implementing Object Undo and Redo capabilities in .NET [^]

Keep in mind that what's shown here ... what a literal interpretation of your question suggests ... is a stack, a last-in-first-out collection, with a depth of #1. If your goal is to have more than one saved state, consider using the .NET 'Stack data structure.

Using the C# 7 ValueTuple Type [^] , we can make this easy:
C#
private (int Nmber, string Strg) _previousData;

public (int Nmber, string Strg) PreviousData // read-only
{
    get { return _previousData; }
}

private (int Nmber, string Strg) _currentData;

public (int Nmber, string Strg) CurrentData
{
    set
    {
        _previousData = (_currentData.Nmber, _currentData.Strg);
        _currentData = value;
    }
    get { return _currentData; }
}


// test: put in some method or EventHandler
// single-step through the code and observe the values

CurrentData = (100, "x");
CurrentData = (200, "y");

var previous = PreviousData;

int prevNum = previous.Nmber;
string prevStrg = previous.Strg;
 
Share this answer
 
v3
Comments
Maciej Los 4-Jan-19 8:00am    
Nice and complete answer.
5ed!
class test{
private int _previousSalary;
private int _salary;

public int salary
{
    get { return _salary; }
    set 
    {
        _previousSalary = _salary;
        _salary = value;
    }
}


Repeat the same idea with month.
 
Share this answer
 
Comments
BillWoodruff 3-Jan-19 11:24am    
with this, the previous value will never be visible to consumers of the class
F-ES Sitecore 3-Jan-19 12:45pm    
If they want it to be public they can add a public property for it. The import thing is that properties don't have to be auto-properties, once the OP understands that they can hopefully come up with the rest of the solution themselves. Give a man a fish and all that.
BillWoodruff 3-Jan-19 13:53pm    
"Give a man a fish and all that." Give a man a sloppy drawing of a fish and then ...
C#
public  class test
{
  public int salary{get;set;}
  public string month{get;set;}
}
public class Execute()
{
  //Instantiating test object
  test t  = new test();
  //Creating a list of tests 
  //so we can store the previous values
  List<test> ListOfTests = new List<test>();
//initializing
 t.salary=10;
 t.month="apr";
 ListOfTests.Add(t);
//modifying
 t.salary=15;
 t.month="mar";
 ListOfTests.Add(t);
}
//the previous values now are stored inside the List of tests
 
Share this answer
 
Comments
BillWoodruff 3-Jan-19 11:21am    
this code will not preserve the first values assigned to 'salary and 'month ! you are modifying only one instance of 'test, adding it twice to the List. go back and study what a 'reference type is.

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