PC Review


Reply
Thread Tools Rate Thread

Comparison of 2 GetValue() calls seems incorrect

 
 
=?Utf-8?B?Qnlyb24=?=
Guest
Posts: n/a
 
      12th Jun 2006
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.

Any help would be greatly appreciated.



protected Boolean GetIsDirty()
{
FieldInfo[] fields = this.GetType().GetFields(BindingFlags.NonPublic
| BindingFlags.Instance);
FieldInfo currentField = null;

foreach (FieldInfo oldField in fields)
{
if (oldField.Name.IndexOf("_Old") != -1)
{
currentField =
this.GetType().GetField(oldField.Name.Remove(1, 3), BindingFlags.NonPublic |
BindingFlags.Instance);
if (oldField.GetValue(this) != currentField.GetValue(this))
return true;
}
}

return false;
} // GetIsDirty

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      12th Jun 2006
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
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ods calls business object then method calls ta with output params andy6 Microsoft ASP .NET 2 9th Jun 2006 02:54 AM
Csharp async calls to Web Service that calls C++ :: blocking issue Brian Parker Microsoft C# .NET 10 21st Apr 2006 03:12 AM
Re: Faxes - Computer answers ALL phone calls as fax calls Hal Hostetler [MVP DTS] Windows XP Print / Fax 0 10th Jul 2003 04:15 AM
Re: Faxes - Computer answers ALL phone calls as fax calls Peter R. Fletcher Windows XP Print / Fax 0 9th Jul 2003 05:19 PM
Re: Faxes - Computer answers ALL phone calls as fax calls Groundhog Windows XP Print / Fax 0 9th Jul 2003 09:30 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:17 AM.