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

casting an object to a specific type

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
This question was answered in another thread. Thanks.

"Steve Teeples" wrote:
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

Nov 17 '05 #2

P: n/a
Steve Teeples wrote:
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
Nov 17 '05 #3

P: n/a
I see you're simply converting everything to a double and managing it that
way. Simple and dependable. Thanks for the insight.

"news.tiscalinet.ch" wrote:
Steve Teeples wrote:
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

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.