By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,666 Members | 1,413 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,666 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.