Comparing 2 values with different type.

  • Thread starter Thread starter Evgeny
  • Start date Start date
E

Evgeny

Hi, all!
I have a litle question about comparing 2 values with different types, f.e
object Value1 = GetDecimalValue() - return decimal value = 1.
object Value2 = GetIntegerValue() - return integer value = 1.

bool res = Value1.Equals(Value2) - return false!!!
bool res2 = Value1 == Value2 - return false!!!
I understand, that a problem is definition Value1 and Value2 as object.
Should i write my function to change types of variables before comparing?
May be anybody knows a better way?

Thanks
Evgeny
 
Evgeny said:
Hi, all!
I have a litle question about comparing 2 values with different types, f.e
object Value1 = GetDecimalValue() - return decimal value = 1.
object Value2 = GetIntegerValue() - return integer value = 1.

bool res = Value1.Equals(Value2) - return false!!!
bool res2 = Value1 == Value2 - return false!!!
I understand, that a problem is definition Value1 and Value2 as object.
Should i write my function to change types of variables before comparing?
May be anybody knows a better way?

Thanks
Evgeny

As you have realised this is a problem that you get if a generic class can
store multiple types and you want to compare instances. A classic instance
of this is using variants in languages such as C++ that only offer very
limited support for variants (MFC only supplies a thin wrapper for
constructing them, and changing their type). The solution I used was to
write a routine like SuperCompare( objectref o1, objectref o2 ) with nested
switch statements that considered every combination of object types that I
was interested in and returned neg/0/pos according to the relationship
between the values ( int MyVariantDeriv::Compare ( MyVariantDeriv *v ); ).
Naturally such a function will need to be very quick and efficient if you
are going to call it lots and you need a really big reason for going down
this route unless the type-pool is v small.

Tom

Tom

Tom
 
Thanks, Tom

Meanwhile i wrote this method....

Heinz Kiosk said:
As you have realised this is a problem that you get if a generic class can
store multiple types and you want to compare instances. A classic instance
of this is using variants in languages such as C++ that only offer very
limited support for variants (MFC only supplies a thin wrapper for
constructing them, and changing their type). The solution I used was to
write a routine like SuperCompare( objectref o1, objectref o2 ) with nested
switch statements that considered every combination of object types that I
was interested in and returned neg/0/pos according to the relationship
between the values ( int MyVariantDeriv::Compare ( MyVariantDeriv *v ); ).
Naturally such a function will need to be very quick and efficient if you
are going to call it lots and you need a really big reason for going down
this route unless the type-pool is v small.

Tom

Tom

Tom
 
Back
Top