473,796 Members | 2,826 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4496
Kenneth Baltrinic <ke*****@baltri nic.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.WriteLi ne (x==y);
Console.WriteLi ne (x.Equals(y));
}
}

--
Jon Skeet - <sk***@pobox.co m>
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*****@baltri nic.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********@hot mail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
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*****@baltri nic.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******** *****@TK2MSFTNG P10.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.enso ft-software.com> wrote in message news:<#s******* *******@TK2MSFT NGP09.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********@hot mail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
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*****@baltri nic.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********@hot mail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
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.enso ft-software.com> wrote in message

news:<#s******* *******@TK2MSFT NGP09.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********@hot mail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
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*****@baltri nic.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2208
by: JackC | last post by:
Hi Problem: I wish to use a pimpl to hide implementation/storage of a class (T), but I also want to hold objects of that class (T) in an std::vector<T> (or similar). T is non trivial (i.e. not POD, because it has std::string and other classes requiring proper destruction, although all drill down to POD and strings eventually). Classic code layout:
5
1504
by: mungflesh | last post by:
I am new to PHP. Can I define an array in PHP 4.3 containing class types? ie. (simplified example) class MyType { function MyFunc() { $newType = new MyType;
1
1975
by: Dany | last post by:
I am trying to find a nice way to compare objects (tables, colums, indexes, primary and fk) between my production and my test dabatase. I would like to known if their are tricks or tools available to help me with this besides starting to compare systems tables contents?
3
8901
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String End Structure
6
1824
by: Anders Würtz | last post by:
i have an assignment to iterate through a collection containing different types of numeric values (float, double, int, byte, short etc.) and to add 1 to all of them. I tried with array and arraylist so far, but i either get a cast exception or the value doesn't seem to get updated at all. What should i be doing to make it work? Thanks in advance Anders
0
1388
by: mseeger | last post by:
My concrete problem is this (it is a bit special, but there may be other instances of it): I'd like to create a class for vectors (say: Vector), exporting math. methods, say v.foo(...). I'd like to implement 'operator()' s.t. I can treat parts of a vector in the same way as a normal vector: if v.foo(...) works, so should v(rng).foo(...), where rng is a range object. This should work even if 'foo' is a non-const method. I do this by...
0
953
by: rn5a | last post by:
A user control file named Address.ascx has 4 TextBoxes. The logic of this user control is encapsulated in a code-behind file named Address.ascx.vb. The code-behind also defines an Event named "TextChangedEvent" which handles the TextChanged event of the 4 TextBoxes. Namespace Address Public Class AddressCB : Inherits UserControl Public txt1 As TextBox Public txt2 As TextBox
15
3538
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of pointers of the base class type and then allocate each object dynamically with 'new' and store the...
9
1481
by: MZimmerman6 | last post by:
I am trying to write a program that reads through a word document, examines tables, copies their contents to the clipboard and saves them into lists of lists: List<List<String>> and also one List<List<List<String>>> Think of it kind of like filing intructions into a list of how to build a dining set or something. The set has different parts, which each will have their own instructions. Dining Set - Table - Step 1 -...
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.