469,085 Members | 1,039 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,085 developers. It's quick & easy.

Getting around reference equality to compare objects containing base types.

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[i] and a[i] 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[i].GetType()?
if( d[i] == a[i] )
{
...
}
}
}
Nov 15 '05 #1
8 4299
Kenneth Baltrinic <ke*****@baltrinic.com> wrote:
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[i] and a[i] 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[i].GetType()?
if( d[i] == a[i] )
{
...
}
}
}


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));
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi,

You can use either

d[i].Equals( a[i] )
or
object.Equals(d[i], a[i])

Hope this helps

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
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" <ke*****@baltrinic.com> wrote in message
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[i] and a[i] 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[i].GetType()?
if( d[i] == a[i] )
{
...
}
}
}

Nov 15 '05 #4
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"Glen Jones" <gl********@hotmail.com> wrote in message
news:ba**************************@posting.google.c om...
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" <ke*****@baltrinic.com> wrote in message
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[i] and a[i] 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[i].GetType()?
if( d[i] == a[i] )
{
...
}
}
}

Nov 15 '05 #5
Thanks for all the help guys but check me on this. Chris's suggestion that
the static member object.Equals(d[i],a[i]) 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

"Chris Taylor" <ch*************@hotmail.com> wrote in message
news:OW*************@TK2MSFTNGP10.phx.gbl...
Hi,

You can use either

d[i].Equals( a[i] )
or
object.Equals(d[i], a[i])

Hope this helps

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #6
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #7
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" <er**@cc.ensoft-software.com> wrote in message news:<#s**************@TK2MSFTNGP09.phx.gbl>...
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"Glen Jones" <gl********@hotmail.com> wrote in message
news:ba**************************@posting.google.c om...
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" <ke*****@baltrinic.com> wrote in message
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[i] and a[i] 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[i].GetType()?
if( d[i] == a[i] )
{
...
}
}
}

Nov 15 '05 #8
dont forget about String.Compare... lexically compares the strings
--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]

"Glen Jones" <gl********@hotmail.com> wrote in message
news:ba**************************@posting.google.c om...
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" <er**@cc.ensoft-software.com> wrote in message

news:<#s**************@TK2MSFTNGP09.phx.gbl>...
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"Glen Jones" <gl********@hotmail.com> wrote in message
news:ba**************************@posting.google.c om...
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" <ke*****@baltrinic.com> wrote in message
> 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[i] and
a[i] 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[i].GetType()?
> if( d[i] == a[i] )
> {
> ...
> }
> }
> }

Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by mungflesh | last post: by
3 posts views Thread by Kiran B. | last post: by
reply views Thread by rn5a | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.