Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Trouble with TypeConverter/TypeDescriptor

Trouble with TypeConverter/TypeDescriptor

Scheduled Pinned Locked Moved C#
helpcsharpasp-netcomquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nathan Ridley
    wrote on last edited by
    #1

    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.

    C 1 Reply Last reply
    0
    • N Nathan Ridley

      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.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      N 1 Reply Last reply
      0
      • C Christian Graus

        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 )

        N Offline
        N Offline
        Nathan Ridley
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups