473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Detecting changes to any object

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
5 8820
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

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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jole | last post by:
Hi I'm writing a program that needs to read from a file. In order for the program to be robust, it should somehow check that the file isn't corrupt, or stuffed in any way. For example, that...
1
by: Yasutaka Ito | last post by:
Hi, Given an object, I want to programmatically monitor it for any property changes and method calls. The object can be anything (component, control, etc.), and there is no guarantee that each...
2
by: Simon Harvey | last post by:
Hi all, Is there any easy way to check a field for calues that have changed on a post back. So the page is sent to the user, the user changes some values and I need to know which ones...
5
by: needin4mation | last post by:
Hi, I have an asp.net 1.1 application that populates data from a database. When the user changes data, they have to hit a button to update the data. The data entry form (same form that is...
5
by: Bob | last post by:
Using Vs 2005 and Vb.NET I want to give users a message asking if they're sure they want to close the form if they had made changes to the underlying data. How do I detect if changes had been made...
79
by: VK | last post by:
I wandering about the common proctice of some UA's producers to spoof the UA string to pretend to be another browser (most often IE). Shouldn't it be considered as a trademark violation of the...
7
by: Jerry Spence1 | last post by:
I want to fire an event when the user changes text, but I don't want to fire it when the program changes the text. This must be a very common thing to do, and I was wondering if there was a better...
2
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Greetings, I am trying to find out how to do something that on the surface seems like it should be very simple to do. I have a datagrid with a datatable bound to it using the SetDataBinding...
7
by: cj | last post by:
As your probably aware the datagridview doesn't update it's source datatable with changes to a cell until you move off that cell. Suppose somone makes a change in a cell of my datagridview then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.