Comparison of 2 GetValue() calls seems incorrect

G

Guest

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
 
J

Jon Skeet [C# MVP]

Byron said:
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.
 

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