Trouble with TypeConverter/TypeDescriptor
-
I'm having trouble trying to determine if some arbitrary object can convert to type decimal. The underlying TypeConverters for int and decimal seem to think that an int can't be converted to a decimal. Now we all know that Convert.ToDecimal(someInt32Value) works just fine, but consider the following piece of code:
int n = 5; TypeConverter tc = TypeDescriptor.GetConverter(n); bool canConvert = tc.CanConvertTo(typeof(decimal)); // canConvert = false at this point
Can anyone see what I'm doing wrong? Is this a framework bug, or have I missed something?NATHAN RIDLEY Web Application Developer email: snowdevil [@] gmail.com !! Are you a skilled ASP.Net programmer or web designer and would like to help bring the best new ASP.Net CMS (http://www.sprocketcms.com[^]) into the forefront of the ASP.Net community? Please contact me and let me know.
-
I'm having trouble trying to determine if some arbitrary object can convert to type decimal. The underlying TypeConverters for int and decimal seem to think that an int can't be converted to a decimal. Now we all know that Convert.ToDecimal(someInt32Value) works just fine, but consider the following piece of code:
int n = 5; TypeConverter tc = TypeDescriptor.GetConverter(n); bool canConvert = tc.CanConvertTo(typeof(decimal)); // canConvert = false at this point
Can anyone see what I'm doing wrong? Is this a framework bug, or have I missed something?NATHAN RIDLEY Web Application Developer email: snowdevil [@] gmail.com !! Are you a skilled ASP.Net programmer or web designer and would like to help bring the best new ASP.Net CMS (http://www.sprocketcms.com[^]) into the forefront of the ASP.Net community? Please contact me and let me know.
use ToString and then decimal.TryParse
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
use ToString and then decimal.TryParse
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Thanks Christian. Seems like a bit of a roundabout way of doing it though. I ended up opting for a small function which simply compares the type to the list of existing types and does the conversion directly if it's a numeric type.
NATHAN RIDLEY Web Application Developer email: snowdevil [@] gmail.com !! Are you a skilled ASP.Net programmer or web designer and would like to help bring the best new ASP.Net CMS (http://www.sprocketcms.com[^]) into the forefront of the ASP.Net community? Please contact me and let me know.