469,568 Members | 1,676 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

casting an object to a specific type

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
3 3448
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
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
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.

Similar topics

4 posts views Thread by Jacob Jensen | last post: by
1 post views Thread by Mark McDonald | last post: by
18 posts views Thread by Marco | last post: by
1 post views Thread by Remco | last post: by
23 posts views Thread by Ren Nordby | last post: by
13 posts views Thread by DaTurk | last post: by
7 posts views Thread by S. Lortan | last post: by
11 posts views Thread by Frederic Rentsch | last post: by
9 posts views Thread by Taras_96 | last post: by
19 posts views Thread by =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.