Byron <(E-Mail Removed)> wrote:
> I have a class that stores values of various types in public properties with
> private fields behind them. In addition to the field that is accessed
> through the public properties there is another private field by the same
> name, preceded with _Old that holds the previous value. So a public property
> of LastName uses a private field named _LastName to store the current value
> and there is another private field named _OldLastName that holds the previous
> LastName value so I can compare the current with the old value to see if it
> has changed.
>
> I then have public read-only Dirty property that calls the method IsDirty
> that runs through all the field looking for any with a name that starts with
> _Old, finds the corresponding field WITHOUT the _Old prefix, and compares the
> result of GetValue() to see if the current and old values are different.
>
> This seems to work fine on strings, but the comparision fails on Booleans
> even though GetValue() returns true for the new and old versions. So in the
> example below, if (oldField.GetValue(this) != currentField.GetValue(this))
> returns true even though both the GetValue() calls return true independently.
That's because the value being returned is boxed, and != is comparing
references. Use the Equals method instead and it should be fine,
although you'll need to check for null references in general.
--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too