By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 2,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

Detecting changes to any object

P: n/a
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 :)
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Lars,

<snip>
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
Nov 16 '05 #2

P: n/a

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

On Fri, 23 Jul 2004 11:54:08 +0200, Marcin Grzębski
<mg*******@taxussi.no.com.spam.pl> wrote:
Hi Lars,

<snip>
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


--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 16 '05 #3

P: n/a

"Lars Moastuen" <la*****@everyday.com> wrote in message news:op**************@lars.mshome.net...
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
Nov 16 '05 #4

P: n/a
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

On Fri, 23 Jul 2004 13:53:36 +0200, Hans Kesting
<ne***********@spamgourmet.com> wrote:

"Lars Moastuen" <la*****@everyday.com> wrote in message
news:op**************@lars.mshome.net...
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


--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 16 '05 #5

P: n/a
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)

On Fri, 23 Jul 2004 14:16:47 +0200, Lars Moastuen <la*****@everyday.com>
wrote:
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

On Fri, 23 Jul 2004 13:53:36 +0200, Hans Kesting
<ne***********@spamgourmet.com> wrote:

"Lars Moastuen" <la*****@everyday.com> wrote in message
news:op**************@lars.mshome.net...
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



--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.