I have a base abstract class that contains a generic dictionary. All of the derived concrete classes inherit this dictionary. The Value of the dictionary is of the base class Type. This is to allow adding/deleting of derived types.
Ok, what I would like to do, is detect when changes have been made in the object model, so that I can implement a "Save" and "Save As..." method. What is available out there that allows me to do something like this

Detecting changes for "Save" and "Save As..." purposes
vien11
Legacyofkain
So, as you can see, this would prove rather difficult rolling through and comparing all of the data in each one of the classes. That is why I was wondering if there was some shortcut method to detect changes.
Hopefully this gives everyone a better concept of my question.
Clinton Chau
If you don't care for efficiency, and your classes are serializable, you could always serialize the classes to a MemoryStream and compute a hash (MD5, SHA1, take your pick). It is theoretically possible that two configuration files share the same hash, but the probability is negligible. While this is probably as inefficient as it gets, it is at least short to write, which seems to be your main requirement.
HTH
--mc
Zapp
Gchris
elwood
public String FirstName
{
get
{
return _firstname;
}
set
{
if( !_firstname.Equals( value ) )
{
_firstname = value;
MarkDirty();
}
}
}
public void Add( OrderLine line )
{
_innerCollection.Add( line );
MarkDirty();
}
Gosovic
Robot r = new Robot();
Robot r2d2 = r;
r.Add(new Arm("left");
r.Arms["left"].length = 23;
The following would result in true:
r.Equals(r2d2)
If I'm misunderstanding something here, please tell me, but that is how I understand this.
Amit Bansal
mattdawg
For your custom object type, you have to implement the Equal() method yourself to tell in what condition the two objects of that class is equal.
public class Robot
{
private string name = "";
public override bool Equals(object obj)
{
// write your code here
// return true if equals, fasle if not
// for example
return (this.name == ((Robot)obj).name) ;
}
}
Reference:
ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_csref/html/7e4c24c5-7693-4c45-88fb-ba5204fbcb20.htm
Gaurav_Gaur
I don't think there's any kind of short cut.
In http://msdn2.microsoft.com/en-us/library/bsc2ak47.aspx
Notes to Implementers This method can be overridden by a derived class. For example, many of the base data types return true if both objects represent the same value; otherwise, false. This method only compares primitives and objects. It must be overridden to compare more complex structures, such as arrays of objects. The following statements must be true for all implementations of the Equals method. In the list, x, y, and z represent object references that are not a null reference (Nothing in Visual Basic).
Hope this can answer your problem.
kunal.pandya
And, although I don't recommend the following idea, if you really have many properties that you want to compare. Think about putting them into any array, or Collections, for example, like a Hashtable.
Basically, you can put all your object properties in a hashtable, which is a prive member of the class. Next is to hide and encapsulate it with Get Set method for the properties. When doing comparison in the Equals() method, what you have to do is loop through the hashtable, and compare the value one by one.
But in terms of design, I am not sure whether it is a "best practice".