473,463 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How test if System.Object's are same type and equal?

I have variables that are declared as objects. Ints, strings and other data
types are then assigned to these objects.

How can I write a compare objects function? I tried objOne==objTwo. I am
guessing that this must be comparing references and not values because it
returns false.

// Test case
objOne = strName1;
objTwo = strName2;
if( CompareObjects(objOne,objTwo) == true)
{
WriteLine("Objects are of same type and equal");
}

// Object Compare function
bool CompareObjects(object objOne, object objTwo)
{
bool bCompareResult = false;
if( objOne == objTwo)
{
bCompareResult = true;
}
return bCompareResult;
}
I have started down the path of using System.Type.GetTypeCode of the
object's type and then making a big hairy switch statement that casts each
object to the appropriate type before making the comparison. Something
like..

switch( System.Type.GetTypeCode(objOne.GetType()))
{
case System.TypeCode.Int32:
bCompareResult = (int)objOne== (int)objTwo;
break;
case System.TypeCode.String:
bCompareResult = (string)objOne== (string)objTwo;
break;
Nov 13 '05 #1
2 18198
Ed Sutton <es*****@nomadics.com> wrote:
I get the feeling that there must be a built-in way of doing object
comparisons.


Have a look at Object.Equals(Object)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2
Check in the document of .NET.
You have example for keyword [is], [typeof], and [as]
Maybe some of these keyword can give you a help!

Stephane
"Ed Sutton" <es*****@nomadics.com> wrote in message
news:OO**************@TK2MSFTNGP11.phx.gbl...
I have variables that are declared as objects. Ints, strings and other data types are then assigned to these objects.

How can I write a compare objects function? I tried objOne==objTwo. I am
guessing that this must be comparing references and not values because it
returns false.

// Test case
objOne = strName1;
objTwo = strName2;
if( CompareObjects(objOne,objTwo) == true)
{
WriteLine("Objects are of same type and equal");
}

// Object Compare function
bool CompareObjects(object objOne, object objTwo)
{
bool bCompareResult = false;
if( objOne == objTwo)
{
bCompareResult = true;
}
return bCompareResult;
}
I have started down the path of using System.Type.GetTypeCode of the
object's type and then making a big hairy switch statement that casts each
object to the appropriate type before making the comparison. Something
like..

switch( System.Type.GetTypeCode(objOne.GetType()))
{
case System.TypeCode.Int32:
bCompareResult = (int)objOne== (int)objTwo;
break;
case System.TypeCode.String:
bCompareResult = (string)objOne== (string)objTwo;
break;
.
.
.
I get the feeling that there must be a built-in way of doing object
comparisons.

Thanks in advance,

-Ed Sutton

Phone: 405-372-9535
Web Site: http://www.nomadics.com/
Nomadics, Inc.
1024 S. Innovation Way
Stillwater, OK 74074
USA

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

467
by: mike420 | last post by:
THE GOOD: 1. pickle 2. simplicity and uniformity 3. big library (bigger would be even better) THE BAD:
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
67
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
9
by: Steve Sargent | last post by:
Hi: I'm trying to debug the following code, and it keeps looping on the if statement: public static bool operator == (OnlineMemberNode first, OnlineMemberNode second) { if(first == null) {
1
by: MariusI | last post by:
I have some business objects which overrides Equals to provide syntax equality instead of just reference equality. Overriding equals gives me a warning that i must override GetHashcode(). Msdn says...
14
by: ThazKool | last post by:
I want to see if this code works the way it should on a Big-Endian system. Also if anyone has any ideas on how determine this at compile-time so that I use the right decoding or encoding...
14
by: serge calderara | last post by:
Dear all, What is the proper way to check if two object are equal ? I do not mean equal on Object type only but also object value's thnaks for help regards serge
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
1
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...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.