E
Ed Sutton
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;
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;