Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing expressions of type object

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?

Doesn't Work

if (row["PersonID"] != tbxPersonID.EditValue)
{
    row["PersonID"] = tbxPersonID.EditValue;
}
if (row["CitizenFlag"] != chkCitizen.EditValue)
{
    row["CitizenFlag"] = chkCitizen.EditValue;
    _whatChanged.Add("CitizenFlag");
}

Works

 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");
 }
like image 578
Refracted Paladin Avatar asked Aug 12 '09 20:08

Refracted Paladin


People also ask

How do you compare objects with objects?

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)

What is the object of comparison?

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.

How do you compare equality of objects?

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.

How can you compare objects in PHP?

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.


4 Answers

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));
    }
}
like image 115
Jon Skeet Avatar answered Oct 14 '22 20:10

Jon Skeet


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

like image 42
Daniel Avatar answered Oct 14 '22 19:10

Daniel


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?

like image 32
pavium Avatar answered Oct 14 '22 18:10

pavium


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.

like image 45
Jason Evans Avatar answered Oct 14 '22 18:10

Jason Evans