473,394 Members | 1,761 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,394 software developers and data experts.

Comparison of 2 GetValue() calls seems incorrect

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

Jun 12 '06 #1
1 1254
Byron <By***@discussions.microsoft.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 12 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
4
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some...
14
by: Santi | last post by:
I see in some code, I donīt remember now if it is c# or c++, that the when they perform a comparison they use the value first and then the variable, like: if( null == variable ){} Is there an...
1
by: meng | last post by:
i have recently been interested in exploring ATL server. i have been doing development on asp.net and server controls the past 1 year or so. in ASP.NET, the Page and ALL its contained controls get...
4
by: Skeets | last post by:
i have an application that should allow acces to linux administrators only. iow, i want to code a php script that will be able to compare the entered password with the linux adminstrator's...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against...
28
by: kaferro | last post by:
What is the safest way to make an argv comparison? The code below works. #include <iostream> #include <string> using namespace std; int main(int argc, char *argv) {
0
by: TraceyAnnison | last post by:
I wonder if you can help me - I'm new to this, and working in a project that has already been configured to work with Axis. We have a Java project built with the Spring framework, in order to...
11
by: Andrus | last post by:
I created dynamic extension methods for <= and < SQL comparison operators: public static IQueryable<TLessThanOrEqual<T>(this IQueryable<Tsource, string property, object value); public static...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.