Sign in to follow this  
Miksan

[.net] Finding if class data has changed

Recommended Posts

I'm trying to implement a system where user is asked to save his changes only if they are made. I've got a huge class with a lot of properties and arrays of more nested classes, so putting a boolean to every property to track changes is a bit too hard. Is there a way of comparing two class instances and finding out if their data is different?

Share this post


Link to post
Share on other sites
It would be easier to simply use one boolean (call it "modified" or something). Anytime you make _any_ changes to the data, you just need to set this one modified boolean to true.

Share this post


Link to post
Share on other sites
I'd say go with strtok's suggestion, but if you wanted to compare two class instances and finding out if their data is different, all you have to do is overload the == operator for that class. Inside there you just do the comparisons and you are all set.

Share this post


Link to post
Share on other sites
Thanks for the quick replies. I think I'll do this by tracking the windows form control's different callbacks (text changed etc.). Oh wait, on a second thought - that doesn't work either :( Everytime user selects different item from a list, texts are changed in the textboxes... It seems that tracking with a bModified flag or something inside the properties set method is the only method. I was thinking about serializing those class instances to memory and then comparing them bit by bit? Is that even possible?

Share this post


Link to post
Share on other sites
hmm, some aspect oriented programming would come in handy at this point :) C# isn't an AOP language, but delegate concatentation allows you to sort of fake it. Just as a crazy solution, you could implement all of your public accessors as delegates (unfortunately, you'd need a unified interface for each accessor), have one method that sets the "modified" boolean, and then concatenate that method delegate with the delegated methods for each of the accessors. Each of the accessor methods would then retain their own logic, completely inoquous that any kind of change tracking is occuring.

As I said, C# is not an AOP language, so you're going to end up writing a lot of background code for such a simple task. I really don't know why I mentioned it, maybe because I'm really tweeking on caffiene right now (WOOOOOOOOOOOOOOOOOOOO!!!!!!!).

In regards to comparing two objects to see if their contents are the same, have the class implement the IComparable interface, and implement the CompareTo method. You'll then have to manually check each value (make sure you don't miss any!), and make your decision based on that.

You obvioulsy can't use the == operator (though you could overload it to call CompareTo(object)).

You could also If you already have a ToString method for this class (just a wild guess) that builds a summary of the data in the class, you could compare the strings returned by both classes--if the classes are different, then their strings won't match. Of course, it's not worth it to implement this method if this is your only intention for it.

Uhm, just thought of something, you are trying to compare one instance of the class to itself in a previous state, you're not trying to compare two different instances of a class. Unless you spawn new instances and save their references whenever you modify the object, you are going to lose that previous state permanently.

MyClass a, b;
a = new MyClass();
b = a;
a.Modify();
a.CompareTo(b); //going to be the same

Share this post


Link to post
Share on other sites
in psuedo code why not do:

//on init
YourClass old( thing);
//possibly modify thing
...
//need to save?
if( thing != old)//not equal do some saving

the overhead of this approach is keeping an extra copy of all data for the life of the possibly modifying block but that shouldn't be to bad in most cases and the solution is dead simple.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this