Getting around reference equality to compare objects containing base types.

K

Kenneth Baltrinic

I am trying to compare values coming out of a database record with known
default values. The defaults are in an array of type object (because they
can be of any basic data type, I am not working with weird stuff, just
strings, int, bools and DataTime values) My fields values for this record,
for convenience are also in an array of objects. Now I am trying to write
code like the following.

private void processData (object[] d, object[] a) // my Default values and
by Actual values
{
for(int i = 0; i < a.length; i++)
{
//problem, == is doing reference equality, I need value equality.
//For any give value i, it is safe to assume that d and a are
//of the same primitive data type, either int, long, single, double,
//bool, DateTime or string. Can I do this without having to do
//if else based on d.GetType()?
if( d == a )
{
...
}
}
}
 
J

Jon Skeet [C# MVP]

Kenneth Baltrinic said:
I am trying to compare values coming out of a database record with known
default values. The defaults are in an array of type object (because they
can be of any basic data type, I am not working with weird stuff, just
strings, int, bools and DataTime values) My fields values for this record,
for convenience are also in an array of objects. Now I am trying to write
code like the following.

private void processData (object[] d, object[] a) // my Default values and
by Actual values
{
for(int i = 0; i < a.length; i++)
{
//problem, == is doing reference equality, I need value equality.
//For any give value i, it is safe to assume that d and a are
//of the same primitive data type, either int, long, single, double,
//bool, DateTime or string. Can I do this without having to do
//if else based on d.GetType()?
if( d == a )
{
...
}
}
}


Use .Equals:

using System;

public class Test
{
static void Main()
{
object x = 10;
object y = 10;
Console.WriteLine (x==y);
Console.WriteLine (x.Equals(y));
}
}
 
G

Glen Jones

Ken,

This applies to base C# objects.

Once you have the array element, cast the element to the IComparable
interface.
Then use the CompareTo() method. This will work for all datatypes
except string.
If you want to check a string you will need to do it the hard way by
casting to a string then checking each char value.

If you need an example after you try it, let me know.

Glen Jones MCSD
 
E

Eric Newton

Ah, the string class does have an Equals override so I would assume that the
String.Equals would check character by character for you...


--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

Glen Jones said:
Ken,

This applies to base C# objects.

Once you have the array element, cast the element to the IComparable
interface.
Then use the CompareTo() method. This will work for all datatypes
except string.
If you want to check a string you will need to do it the hard way by
casting to a string then checking each char value.

If you need an example after you try it, let me know.

Glen Jones MCSD

Kenneth Baltrinic said:
I am trying to compare values coming out of a database record with known
default values. The defaults are in an array of type object (because they
can be of any basic data type, I am not working with weird stuff, just
strings, int, bools and DataTime values) My fields values for this record,
for convenience are also in an array of objects. Now I am trying to write
code like the following.

private void processData (object[] d, object[] a) // my Default values and
by Actual values
{
for(int i = 0; i < a.length; i++)
{
//problem, == is doing reference equality, I need value equality.
//For any give value i, it is safe to assume that d and a are
//of the same primitive data type, either int, long, single, double,
//bool, DateTime or string. Can I do this without having to do
//if else based on d.GetType()?
if( d == a )
{
...
}
}
}
 
K

Kenneth Baltrinic

Thanks for all the help guys but check me on this. Chris's suggestion that
the static member object.Equals(d,a) would not would it seems to me
because it only checks reference equality. The instance member version
works because in this case the method is overridden by the implementing base
type object wrappers correct?
--Ken
 
C

Chris Taylor

Hi Kenneth,

The Object.Equals( object a, object b ) static member works and provides
some nice safety catches that you would otherwise have implement your
self.

1)Checks that a and b are not null references
If both are null it returns true
If one of them is null it returns false
If the references are the same returns true

2)If step 1 is completed with out returning i.e neither
reference is null and the references are not equal, it proceeds to call
a.Equals( b ) allowing the object to perform a more specific test, and
returns the result.

This reduces the amount of code that you are required to write since you
no longer have to check if the object you are invoking the Equals method
on is null, also as an initial test it does check the references for
equality since if the references are equal the objects must be equal,
reducing the *possibility* of a *potentially* less efficient object
level comparison.

Hope this helps

Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
 
G

Glen Jones

Eric,

Your right, strings do support equals but in case he wanted they
wanted to check < or > I suggested what I did. And truthfully I didn't
want to expain al that.

I don't know why they didn't add support for full string comparison.

Glen Jones MCSD

Eric Newton said:
Ah, the string class does have an Equals override so I would assume that the
String.Equals would check character by character for you...


--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

Glen Jones said:
Ken,

This applies to base C# objects.

Once you have the array element, cast the element to the IComparable
interface.
Then use the CompareTo() method. This will work for all datatypes
except string.
If you want to check a string you will need to do it the hard way by
casting to a string then checking each char value.

If you need an example after you try it, let me know.

Glen Jones MCSD

Kenneth Baltrinic said:
I am trying to compare values coming out of a database record with known
default values. The defaults are in an array of type object (because they
can be of any basic data type, I am not working with weird stuff, just
strings, int, bools and DataTime values) My fields values for this record,
for convenience are also in an array of objects. Now I am trying to write
code like the following.

private void processData (object[] d, object[] a) // my Default values and
by Actual values
{
for(int i = 0; i < a.length; i++)
{
//problem, == is doing reference equality, I need value equality.
//For any give value i, it is safe to assume that d and a are
//of the same primitive data type, either int, long, single, double,
//bool, DateTime or string. Can I do this without having to do
//if else based on d.GetType()?
if( d == a )
{
...
}
}
}
 
E

Eric Newton

dont forget about String.Compare... lexically compares the strings


--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

Glen Jones said:
Eric,

Your right, strings do support equals but in case he wanted they
wanted to check < or > I suggested what I did. And truthfully I didn't
want to expain al that.

I don't know why they didn't add support for full string comparison.

Glen Jones MCSD

"Eric Newton" <[email protected]> wrote in message
Ah, the string class does have an Equals override so I would assume that the
String.Equals would check character by character for you...


--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

Glen Jones said:
Ken,

This applies to base C# objects.

Once you have the array element, cast the element to the IComparable
interface.
Then use the CompareTo() method. This will work for all datatypes
except string.
If you want to check a string you will need to do it the hard way by
casting to a string then checking each char value.

If you need an example after you try it, let me know.

Glen Jones MCSD

I am trying to compare values coming out of a database record with known
default values. The defaults are in an array of type object
(because
they
can be of any basic data type, I am not working with weird stuff, just
strings, int, bools and DataTime values) My fields values for this record,
for convenience are also in an array of objects. Now I am trying to write
code like the following.

private void processData (object[] d, object[] a) // my Default
values
and
by Actual values
{
for(int i = 0; i < a.length; i++)
{
//problem, == is doing reference equality, I need value equality.
//For any give value i, it is safe to assume that d and
a
are
//of the same primitive data type, either int, long, single, double,
//bool, DateTime or string. Can I do this without having to do
//if else based on d.GetType()?
if( d == a )
{
...
}
}
}
 

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