Detecting changes to any object

L

Lars Moastuen

Hi!

I'm currently playing around with a project where I need to know if an
object (any object) has been altered since last check. I need this to know
when an object needs to be saved... My idea was to represent the object as
a byte-array and then make a MD5-hash out of that byte[] (not very
efficient though - but that won't really be a big problem here)... I guess
that works fine as long as the object I need to check for updates doesn't
contain any pointers to other objects, because I do not want changes in
"sub-objects" to affect the state of "this" object, as the "sub-object"
will be handled seperatly. The way I thought this could be done would be
to take the address of the sub-object instead of the content of that
object... I'll try to explain with an example:

class SomeClass
{
private int aInt; // Changes should be detected
private String aString; // Changes should be detected
private Object aObject; // Changes to aObject should NOT be detected,
only changes in address of aObject
}

I've tried using the &-operator to get the address of aObject, but that
just gives:
"CS0208: Cannot take the address or size of a variable of a managed type
('object')"
so that won't work...

Does anyone have any ideas on how I can detect changes to an object
without having to write code in all methods changing an object?

Help appreciated,
Lars Moastuen :)
 
G

Guest

Hi Lars,

class SomeClass
{
private int aInt; // Changes should be detected
private String aString; // Changes should be detected
private Object aObject; // Changes to aObject should NOT be
detected, only changes in address of aObject
}

I've tried using the &-operator to get the address of aObject, but that
just gives:
"CS0208: Cannot take the address or size of a variable of a managed
type ('object')"
so that won't work...

Object variables are "pointers" (in C++ definition) by default so
you should not use "&" operator. But you can easily compare two object
references (adresses) by:

aObj==bObj
Does anyone have any ideas on how I can detect changes to an object
without having to write code in all methods changing an object?

The common way is to define events for changing properties.
e.g.

public event EventHandler AStringChanged;

public string AString {
get {
return aString;
}
set {
if( aString!=value ) {
aString=value;
if( this.AStringChanged!=null ) {
this.AStringChanged(this, EventArgs.Empty);
}
}
}
}

You can handle this event to refresh UI when
"AString" will be changed.

Regards

Marcin
 
L

Lars Moastuen

Hi

Thanks for the idea using EventHandler's, but that doesn't really get me
far I'm afraid. I would like to be able to use existing classes (for
instance an ArrayList) and detect changes to theese objects as well...

Thanks though :)
Lars Moastuen
 
H

Hans Kesting

Lars Moastuen said:
Hi!

I'm currently playing around with a project where I need to know if an
object (any object) has been altered since last check. I need this to know
when an object needs to be saved...
[snip]

The class "Object" has a GetHashCode() method, which means that every
class has that. Would it be possible to use that?

Get hashcodes for the "current" objects, later get the hashcodes again and
compare them with the stored "original" values: change means that the values have been
changed, but "no change" does not necessarily mean that there was really no change!
(that would depend on the implementation of the particular hashcode algorithm)

Hans Kesting
 
L

Lars Moastuen

Yeah, I wish =) Object's GetHashCode() doesn't seem to change if you
change any values, it only differs from instance to instance (an instance
will always have the same GetHashCode() as long as it lives - atleast in
the most implementations)... Therefor GetHashCode() alone wont be
sufficient...

BUT! If I maybe use GetHashCode() on pointers instead of using the
address, I might be able to incorporate this into my orginal idea...
Danger is that hashcodes are reused, so I risk that an objects hashcode
doesn't change when the object is replaced... How likely this is, I do not
know though...

I'll try to do some testing on this..
---
Lars

Lars Moastuen said:
Hi!

I'm currently playing around with a project where I need to know if an
object (any object) has been altered since last check. I need this to
know
when an object needs to be saved...
[snip]

The class "Object" has a GetHashCode() method, which means that every
class has that. Would it be possible to use that?

Get hashcodes for the "current" objects, later get the hashcodes again
and
compare them with the stored "original" values: change means that the
values have been
changed, but "no change" does not necessarily mean that there was really
no change!
(that would depend on the implementation of the particular hashcode
algorithm)

Hans Kesting
 
L

Lars Moastuen

I've done some testing now, but just syntetic tests, so do not take my
results as the absolute truth :)

The "hash-method" seems to work fine (In my tests, I got no missed changes
what-so-ever...) but is relative slow so it wont be any good when
performance is important, when large object models are to be checked for
changes (> 10.000 objects) or when one need to check for changes
frequently...

In my test-app 10.000 instances are hashed before a change and after a
change (2 hashes per instance) and theese are compared. The test takes
about 2-3 seconds on P3 1.8 GHz including allocations...

Just thought someone, sometime, may wonder about this =)

Lars Moastuen (tjordah I AM NO SPAMMER at start dotty no)

Yeah, I wish =) Object's GetHashCode() doesn't seem to change if you
change any values, it only differs from instance to instance (an
instance will always have the same GetHashCode() as long as it lives -
atleast in the most implementations)... Therefor GetHashCode() alone
wont be sufficient...

BUT! If I maybe use GetHashCode() on pointers instead of using the
address, I might be able to incorporate this into my orginal idea...
Danger is that hashcodes are reused, so I risk that an objects hashcode
doesn't change when the object is replaced... How likely this is, I do
not know though...

I'll try to do some testing on this..
---
Lars

Lars Moastuen said:
Hi!

I'm currently playing around with a project where I need to know if an
object (any object) has been altered since last check. I need this to
know
when an object needs to be saved...
[snip]

The class "Object" has a GetHashCode() method, which means that every
class has that. Would it be possible to use that?

Get hashcodes for the "current" objects, later get the hashcodes again
and
compare them with the stored "original" values: change means that the
values have been
changed, but "no change" does not necessarily mean that there was
really no change!
(that would depend on the implementation of the particular hashcode
algorithm)

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top