Okay, this is probably very simple but, I have the below "checks" (not at the same time)
and the First ALWAYS evaluates to TRUE while the Second SEEMS to work. This actually happens in each place that the row value is a number or bool(Date seems fine...)
.
If I walk through the code in Debug it shows the value of row["PersonID"]
as 162434, the same as tbxPersonID.EditValue
. Is this just a basic and beginner truth about programming that I missed in my hodge-podge-self-education
?
It seems, if I cast everything in question to a string
first, I will be fine I would just like to know if I am correct and if there is a general rule as to what Types
I would need to do this for?
if (row["PersonID"] != tbxPersonID.EditValue)
{
row["PersonID"] = tbxPersonID.EditValue;
}
if (row["CitizenFlag"] != chkCitizen.EditValue)
{
row["CitizenFlag"] = chkCitizen.EditValue;
_whatChanged.Add("CitizenFlag");
}
if (row["PersonID"].ToString() != tbxPersonID.EditValue.ToString())
{
row["PersonID"] = tbxPersonID.EditValue;
}
if (row["CitizenFlag"].ToString() != chkCitizen.EditValue.ToString())
{
row["CitizenFlag"] = chkCitizen.EditValue;
_whatChanged.Add("CitizenFlag");
}
The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address. Syntax: public boolean equals(Object obj)
Object comparison refers to the ability of an object to determine whether it is essentially the same as another object. You evaluate whether one object is equal to another by sending one of the objects an isEqual: message and passing in the other object.
The behavior for performing loose equality using == is as follows: If the operands have the same type, they are compared as follows: Object: return true only if both operands reference the same object. String: return true only if both operands have the same characters in the same order.
PHP has a comparison operator == using which a simple comarison of two objecs variables can be performed. It returns true if both belong to same class and values of corresponding properties are are same.
row["PersonID"]
is of type object, which means that != and == will use reference identity. Basically you're comparing boxed values.
If you use:
if (!object.Equals(row["PersonID"], tbxPersonID.EditValue))
then you'll get value equality semantics, and I suspect you'll be okay - assuming that tbxPersonID really is an int
, either boxed or not.
Just to make things concrete, here's a short but complete example to show what I'm talking about:
using System;
class Test
{
static void Main()
{
object first = 2;
object second = 2;
// Compares reference equality: false
Console.WriteLine(first == second);
// Compares value equality: true
Console.WriteLine(object.Equals(first, second));
}
}
Whats the type of EditValue or the right hand value? The problem is probably that EditValue is not of type string (maybe its a subclass or something) therefore when you are doing != or == on it its doing a memory address compare instead of the compare for a string therefore you are getting false instead of true
I don't know about C#, but in some languages the test of equality is different for numeric values and strings. So here you have to force a string comparison before it works.
Does this tell us something?
Without the call to ToString()
, your code is testing equality on two Objects rather then two numbers, thus the reason why it's not working.
By using ToString()
your explicitly telling the code the get the values from the row[]
and EditValue objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With