Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Shallow" Compare 1

Status
Not open for further replies.

MasterRacker

Active member
Oct 13, 1999
3,343
US
Is there any way to perform a quick object comparison that's similar a shallow copy? In other words, rather than setting up an Equals method to compare all members, I would like to simply see that the two objects are not identical, without caring how they are different or what member(s) are different.

Jeff
[purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day

I was not born cynical - I earned my cynicism through careful observation of the world around me.
 
You can override the GetHashCode method, and just compare that value:
Code:
public int GetHashCode()
{
  return _property1.GetHashCode() ^ _property2.GetHashCode();
}
Be aware that if you have strings in there, the GetHashCode implementation for a string is to walk through the entire string to get it's hash value. So longer the strings, the longer it will take. Same applies for arrays, etc.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
That should work. I have forms bound to single objects. I'm keeping a "before" copy of an object to compare and throw a "Save Changes?" question when the user closes the form. That seemed cleaner than having to set up OnChange checks on every control on the form (some of the forms are pretty populous). Also, I figured that this method would be less susceptible to errors of omission in the future if I add members.

Since there is only a single comparison going on at any given time, performance should not be an issue.

Jeff
[purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day

I was not born cynical - I earned my cynicism through careful observation of the world around me.
 
Oops. Forgot the override keyword. But you knew that, right?

Also, this isn't a true cryptographic hash -- there is a definite possibility of collisions, although the XOR operator minimizes that.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Didn't catch that, but I'd have figured it out. (eventually... ;-) )

This is just a quick and easy check for changes via data entry. The occasional collision would not be a problem.

Jeff
[purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day

I was not born cynical - I earned my cynicism through careful observation of the world around me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top