David Maisuradze
David Maisuradze

Reputation: 844

Compare object to object

I have 2 variables:

object Id = itemType.GetProperty(dataValueField).GetValue(item, null); //=0;

object selectedValue = 0;

And want to compare them.

I have tried:

var a = Id.Equals(selectedValue);
var b = Id == selectedValue;
var c = Object.Equals(Id, selectedValue);
var d = var x = Object.Equals((Object)Id, (Object)selectedValue);

But everything returns false;


Update: Id is typeof Int64? and selectedValue is Int32? but == operator works for Int64? and Int32?

Upvotes: 1

Views: 190

Answers (4)

kha
kha

Reputation: 20023

Have a look at the source code for Int64 and Int32. Excerpt from Int64:

http://www.dotnetframework.org/default.aspx/4@0/4@0/DEVDIV_TFS/Dev10/Releases/RTMRel/ndp/clr/src/BCL/System/Int64@cs/1305376/Int64@cs

    public override bool Equals(Object obj) {
        if (!(obj is Int64)) {
            return false; 
        }
        return m_value == ((Int64)obj).m_value; 
    } 

If you notice, the first thing the Equals does is a type check. The types are not compatible and your only solution will be to either cast the Int32 to an Int64 or write a custom Equals that does what you need.

And some test to demonstrate:

public class Blah
{
    public Int32 Id32 { get; set; }
    public Int64 Id64 { get; set; }
}

private void DoTest()
{
    var blah = new Blah
    {
        Id32 = 1,
        Id64 = 1
    };

    object idInt32 = blah.GetType().GetProperty("Id32").GetValue(blah, null);
    object idInt64 = blah.GetType().GetProperty("Id64").GetValue(blah, null);
    object selectedValue = 1; // default type is Int32

    bool areTheSameInt = idInt32.Equals(selectedValue); // true
    bool areTheSameLong = idInt64.Equals(selectedValue); // false
}

Upvotes: 1

poke
poke

Reputation: 388273

Id is typeof Int64? and selectedValue is Int32? but == operator works for Int64? and Int32?

You are right that those types compare properly:

Int64? Id = 0;
Int32? selectedValue = 0;

Console.WriteLine(Id == selectedValue); // indeed true

However, your types are objects, so you have to keep in mind that what actually happens is a comparison of objects:

object IdAsObject = Id;
object selectedValueAsObject = selectedValue;

Console.WriteLine(IdAsObject.Equals(selectedValueAsObject));
Console.WriteLine(IdAsObject == selectedValueAsObject);
Console.WriteLine(Object.Equals(IdAsObject, selectedValueAsObject));
Console.WriteLine(Object.Equals((Object)IdAsObject, (Object)selectedValueAsObject));

And these are all false, since two objects are only equal if they are the same object.

If you want to compare the values, you should make sure to use proper types.

Upvotes: 2

zanseb
zanseb

Reputation: 1435

You are comparing the instance and thats why they are different. Do you have to use an object? Could you cast it to an int ?

You could use a custom class and override Equals() and the operator ==. Take a look at: https://msdn.microsoft.com/en-us/library/ms173147.aspx

I hope it helps.

Upvotes: 3

Patrick Hofman
Patrick Hofman

Reputation: 157098

I am pretty sure there is a problem with your data types, not the values. If you for example take 0L (long) and an integer with value 0, all ways to check equality return false. If both have the same type (int) for example, it works.

Upvotes: 2

Related Questions