473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 3557
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
1
by: Mark McDonald | last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t understand why you can’t declare an implicit operator to convert a base class to a derived class. The text...
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
13
by: DaTurk | last post by:
Hi, This is a question brought about by a solution I came up with to another question I had, which was "Dynamic object creation". So, I'm curious if you can dynamically cast an object. If you...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
11
by: Frederic Rentsch | last post by:
Hi all, If I derive a class from another one because I need a few extra features, is there a way to promote the base class to the derived one without having to make copies of all attributes? ...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.