How can I determine if a property in an object has changed
.net, c#
Solution
You fortunately don't need to reinvent the wheel as there is something called `INotifyPropertyChanged` that does exactly what you need. It is easy to setup and use and it saves a lot of time. Take a look at this tutorial on how to implement it:
http://csharp.2000things.com/tag/inotifypropertychanged/
Problem
I have a number of objects in my application which have a large amount of properties. These objects represent database rows (a lightweight ORM). I'd like to know if there is any way I can determine if a property in the object has changed. For instance, I realise I 'could' do it like this... ``` // Property for showing us whether a value in this object has changed private bool _HasChanged = false; public bool HasChanged { get { return _HasChanged; } private set { _HasChanged = value; } } // My list of properties private string _FirstName; public string FirstName { get { return _FirstName; } set { _HasChanged = true; _FirstName = value; } } private string _LastName; public string LastName { get { return _LastName; } set { _HasChanged = true; _LastName = value; } } private int _Age; public int Age { get { return _Age; } set { _HasChanged = true; _Age = value; } } ``` But if I use the above example, it means a lot of code for each object (some objects have up to 40 properties, and I have about 30 different objects). What I'd like to do is something more like this... ``` // Property for showing us whether a value in this object has changed private bool _HasChanged = false; public bool HasChanged { get { return _HasChanged; } private set { _HasChanged = value; } } // My list of properties public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } ``` If I could do the above and somehow globally set my HasChanged whenever any property in the object changes, it would really speed up my development process. Any ideas or pointers on how I could do this better would be greatly appreciated. This is in C# using NET 4.0 (though I could use 4.5 if it would help!) Many thanks,