casting an object to a specific type

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a method that passes in two string objects (both numerical numbers)
and a string identifying their type.

public bool DoCompare(string num1, string num2, string theirType)
{
System.Type type = System.Type.GetType(theirType);
return ((type)num1 <= (type)num2) ? true : false;
}

The compiler doesn't like the (type) casting that I do. What is the proper
way of casting an object to its correct type to do such a mathematical
operation?
 
Steve said:
I have a method that passes in two string objects (both numerical numbers)
and a string identifying their type.

public bool DoCompare(string num1, string num2, string theirType)
{
System.Type type = System.Type.GetType(theirType);
return ((type)num1 <= (type)num2) ? true : false;
}

The compiler doesn't like the (type) casting that I do. What is the proper
way of casting an object to its correct type to do such a mathematical
operation?

I was looking at some of your other threads and I saw a REALLY big if,
but here is a simpler version and number safe...

public bool DoCompare( string num1, string num2) {
Decimal num1 = Decimal.Parse( num1);
Decimal num2 = Decimal.Parse( num2);

return Decimal.op_LessThanOrEqual( num1, num2);
}

Christian
 
I see you're simply converting everything to a double and managing it that
way. Simple and dependable. Thanks for the insight.
 
Back
Top