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. Type conversion

Type conversion

Scheduled Pinned Locked Moved C#
question
3 Posts 3 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.
  • R Offline
    R Offline
    robert110
    wrote on last edited by
    #1

    Hello everyone: What is the difference b/wint x = (int)MyValue;
    andint x = System.Convert.ToInt32(MyValue);
    which should be used when and why? Thanks. We are using an automated tool for standard validation and its fussing about (int) way of type conversion, can someone plz explain the difference b/w the two. Thanks. Robert

    P 1 Reply Last reply
    0
    • R robert110

      Hello everyone: What is the difference b/wint x = (int)MyValue;
      andint x = System.Convert.ToInt32(MyValue);
      which should be used when and why? Thanks. We are using an automated tool for standard validation and its fussing about (int) way of type conversion, can someone plz explain the difference b/w the two. Thanks. Robert

      P Offline
      P Offline
      philip_cole
      wrote on last edited by
      #2

      Hi Robert Both ways do similar things, but it depends what the actual Type of MyValue is. (int)MyValue is casting MyValue to type int. This means that MyValue has to be an int in the first place, or a type inherited from int (which isn't possible of course!). This is a faster method, because very little has to be checked or changed in memory, but you have to know that MyValue is actually an int. Convert.ToInt32(MyValue) allows you to create an int from other types, e.g. a string like "56". This is useful if you aren't sure what type MyValue is. Because it has to work out what to do with the value tho, this method can be a lot slower than casting directly. For more info, see MSDN: Casting in C#: http://msdn2.microsoft.com/en-us/library/ms173105(VS.80).aspx Convert Class: http://msdn2.microsoft.com/en-us/library/system.convert.aspx Hope this helps Phil

      A 1 Reply Last reply
      0
      • P philip_cole

        Hi Robert Both ways do similar things, but it depends what the actual Type of MyValue is. (int)MyValue is casting MyValue to type int. This means that MyValue has to be an int in the first place, or a type inherited from int (which isn't possible of course!). This is a faster method, because very little has to be checked or changed in memory, but you have to know that MyValue is actually an int. Convert.ToInt32(MyValue) allows you to create an int from other types, e.g. a string like "56". This is useful if you aren't sure what type MyValue is. Because it has to work out what to do with the value tho, this method can be a lot slower than casting directly. For more info, see MSDN: Casting in C#: http://msdn2.microsoft.com/en-us/library/ms173105(VS.80).aspx Convert Class: http://msdn2.microsoft.com/en-us/library/system.convert.aspx Hope this helps Phil

        A Offline
        A Offline
        aSarafian
        wrote on last edited by
        #3

        philip_cole wrote:

        (int)MyValue is casting MyValue to type int. This means that MyValue has to be an int in the first place, or a type inherited from int (which isn't possible of course!). This is a faster method, because very little has to be checked or changed in memory, but you have to know that MyValue is actually an int.

        Some remarks and I would appriciate your input. (int)Something conversion is applicable when Something is a numeric type. It just does the convertion without caring what data is lost. I did sometesting and here is what i came across string a1="5"; int b1=(int)a1; result in a compiler error object a1="5"; int b1=(int)a1; result in runtime casting exception Error.

        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