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. Other Discussions
  3. The Weird and The Wonderful
  4. Convert.ToInt32

Convert.ToInt32

Scheduled Pinned Locked Moved The Weird and The Wonderful
visual-studiocom
29 Posts 13 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 Ron Beyer

    What do you expect? Convert.ToInt32(Int32) does nothing, you are trying to point it out as a joke, but its actually quite useful especially when you don't know the type that you are passing into it (which is why Convert has so many ToXXX overloads). Convert.ToDouble(Double) is the same, as well as Convert.ToSingle(Single), etc.

    S Offline
    S Offline
    Silvabolt
    wrote on last edited by
    #4

    Hmm... totally missed this usage case, you're right. More often than not though, I use the Convert class to convert objects where I do know what I'm passing in, so it just struck me as odd when I was browsing documentation. I feel like they could have mentioned this in the remarks rather than a 'nothing happened' remark.

    R P 2 Replies Last reply
    0
    • S Silvabolt

      Hmm... totally missed this usage case, you're right. More often than not though, I use the Convert class to convert objects where I do know what I'm passing in, so it just struck me as odd when I was browsing documentation. I feel like they could have mentioned this in the remarks rather than a 'nothing happened' remark.

      R Offline
      R Offline
      Ron Beyer
      wrote on last edited by
      #5

      There are even more usage scenarios than that, take for example the following code:

      public void SomeDummyMethod(int myNumber)
      {
      double myDouble = Convert.ToDouble(myNumber);

      return myDouble \* 1000.1f;
      

      }

      Now, lets say later down the line you get somebody who says "wait, myNumber needs to be a signed byte!"... Well now you only have to change one line of code:

      public void SomeDummyMethod(SByte myNumber)
      {
      double myDouble = Convert.ToDouble(myNumber);

      return myDouble \* 1000.1f;
      

      }

      Then later somebody comes around and says, "no, it should be a double to begin with"...

      public void SomeDummyMethod(double myNumber)
      {
      double myDouble = Convert.ToDouble(myNumber);

      return myDouble \* 1000.1f;
      

      }

      This is an overly simplified case obviously, but imagine if there were 100 or 200 lines of code in the function, if they didn't have Convert.ToDouble(double) the one change at the top would break unknown lines of code below. Plus, the design strategy for the class was Convert should convert from any numeric type any other numeric type. Oddly enough that also means converting from something back to itself... On top of all that, it really helps support the IConvertable [^]interface later on, and even says in the documentation: "The common language runtime typically exposes the IConvertible interface through the Convert class. The common language runtime also uses the IConvertible interface internally, in explicit interface implementations, to simplify the code used to support conversions in the Convert class and basic common language runtime types." So much more useful than you think :)

      K Richard DeemingR 2 Replies Last reply
      0
      • S Silvabolt

        Hmm... totally missed this usage case, you're right. More often than not though, I use the Convert class to convert objects where I do know what I'm passing in, so it just struck me as odd when I was browsing documentation. I feel like they could have mentioned this in the remarks rather than a 'nothing happened' remark.

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #6

        Best to avoid the Convert class generally; the only useful member is ChangeType.

        1 Reply Last reply
        0
        • R Ron Beyer

          There are even more usage scenarios than that, take for example the following code:

          public void SomeDummyMethod(int myNumber)
          {
          double myDouble = Convert.ToDouble(myNumber);

          return myDouble \* 1000.1f;
          

          }

          Now, lets say later down the line you get somebody who says "wait, myNumber needs to be a signed byte!"... Well now you only have to change one line of code:

          public void SomeDummyMethod(SByte myNumber)
          {
          double myDouble = Convert.ToDouble(myNumber);

          return myDouble \* 1000.1f;
          

          }

          Then later somebody comes around and says, "no, it should be a double to begin with"...

          public void SomeDummyMethod(double myNumber)
          {
          double myDouble = Convert.ToDouble(myNumber);

          return myDouble \* 1000.1f;
          

          }

          This is an overly simplified case obviously, but imagine if there were 100 or 200 lines of code in the function, if they didn't have Convert.ToDouble(double) the one change at the top would break unknown lines of code below. Plus, the design strategy for the class was Convert should convert from any numeric type any other numeric type. Oddly enough that also means converting from something back to itself... On top of all that, it really helps support the IConvertable [^]interface later on, and even says in the documentation: "The common language runtime typically exposes the IConvertible interface through the Convert class. The common language runtime also uses the IConvertible interface internally, in explicit interface implementations, to simplify the code used to support conversions in the Convert class and basic common language runtime types." So much more useful than you think :)

          K Offline
          K Offline
          krumia
          wrote on last edited by
          #7

          public void SomeDummyMethod(double myNumber)
          {
          double myDouble = Convert.ToDouble(myNumber);

          return myDouble \* 1000.1f;
          

          }

          And after few months someone finds this code and posts it to Weird and Wonderful. ;P

          R 1 Reply Last reply
          0
          • K krumia

            public void SomeDummyMethod(double myNumber)
            {
            double myDouble = Convert.ToDouble(myNumber);

            return myDouble \* 1000.1f;
            

            }

            And after few months someone finds this code and posts it to Weird and Wonderful. ;P

            R Offline
            R Offline
            Ron Beyer
            wrote on last edited by
            #8

            What, you can't return a double as void? :D

            1 Reply Last reply
            0
            • R Ron Beyer

              There are even more usage scenarios than that, take for example the following code:

              public void SomeDummyMethod(int myNumber)
              {
              double myDouble = Convert.ToDouble(myNumber);

              return myDouble \* 1000.1f;
              

              }

              Now, lets say later down the line you get somebody who says "wait, myNumber needs to be a signed byte!"... Well now you only have to change one line of code:

              public void SomeDummyMethod(SByte myNumber)
              {
              double myDouble = Convert.ToDouble(myNumber);

              return myDouble \* 1000.1f;
              

              }

              Then later somebody comes around and says, "no, it should be a double to begin with"...

              public void SomeDummyMethod(double myNumber)
              {
              double myDouble = Convert.ToDouble(myNumber);

              return myDouble \* 1000.1f;
              

              }

              This is an overly simplified case obviously, but imagine if there were 100 or 200 lines of code in the function, if they didn't have Convert.ToDouble(double) the one change at the top would break unknown lines of code below. Plus, the design strategy for the class was Convert should convert from any numeric type any other numeric type. Oddly enough that also means converting from something back to itself... On top of all that, it really helps support the IConvertable [^]interface later on, and even says in the documentation: "The common language runtime typically exposes the IConvertible interface through the Convert class. The common language runtime also uses the IConvertible interface internally, in explicit interface implementations, to simplify the code used to support conversions in the Convert class and basic common language runtime types." So much more useful than you think :)

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #9

              Ron Beyer wrote:

              public void SomeDummyMethod(int myNumber) {     double myDouble = Convert.ToDouble(myNumber);     return myDouble * 1000.1f; }

              Apart from the fact that you can't return a value from a void method, why are you multiplying a double by a float constant? ;P


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              OriginalGriffO 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Ron Beyer wrote:

                public void SomeDummyMethod(int myNumber) {     double myDouble = Convert.ToDouble(myNumber);     return myDouble * 1000.1f; }

                Apart from the fact that you can't return a value from a void method, why are you multiplying a double by a float constant? ;P


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #10

                He doesn't want it to sink without trace...

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                L 1 Reply Last reply
                0
                • S Silvabolt

                  Convert.ToInt32(Int32)[^]

                  Quote:

                  Returns the specified 32-bit signed integer; no actual conversion is performed.

                  Most useful method evar....

                  A Offline
                  A Offline
                  Argonia
                  wrote on last edited by
                  #11

                  By M$ logic the point of using that kind of "useful" methods is when you call Convert.ToInt32 with object, dynamic or var variable you will always get Int32 as a result. P.S. Today i found one more reason to dislike c#. Who the hell had the brilliant idea to make Tuples read only ? And why the elephant ?

                  Microsoft ... the only place where VARIANT_TRUE != true

                  Richard DeemingR R R J 4 Replies Last reply
                  0
                  • A Argonia

                    By M$ logic the point of using that kind of "useful" methods is when you call Convert.ToInt32 with object, dynamic or var variable you will always get Int32 as a result. P.S. Today i found one more reason to dislike c#. Who the hell had the brilliant idea to make Tuples read only ? And why the elephant ?

                    Microsoft ... the only place where VARIANT_TRUE != true

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #12

                    Argonia wrote:

                    Who the hell had the brilliant idea to make Tuples read only ? And why the elephant ?

                    The design of the System.Tuple classes is more to do with the BCL team than the C# team. Any .NET language which uses these classes will get the same read-only behaviour. And they're read-only because they originated in functional programming, where pretty much everything is immutable.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    1 Reply Last reply
                    0
                    • A Argonia

                      By M$ logic the point of using that kind of "useful" methods is when you call Convert.ToInt32 with object, dynamic or var variable you will always get Int32 as a result. P.S. Today i found one more reason to dislike c#. Who the hell had the brilliant idea to make Tuples read only ? And why the elephant ?

                      Microsoft ... the only place where VARIANT_TRUE != true

                      R Offline
                      R Offline
                      Rob Grainger
                      wrote on last edited by
                      #13

                      Actually, tuples largely come from functional languages where everything is immutable. There are all sorts of benefits - for example immutable objects are guaranteed to be thread-safe, are simpler to reason about, and offer higher security than mutable objects. Also, importantly, if they were mutable, by definition they wouldn't be tuples. If you want a mutable, ordered collection of elements, write one and call it something else! Wikiepdia has a good entry on immutable objects[^].

                      "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                      1 Reply Last reply
                      0
                      • R Ron Beyer

                        What do you expect? Convert.ToInt32(Int32) does nothing, you are trying to point it out as a joke, but its actually quite useful especially when you don't know the type that you are passing into it (which is why Convert has so many ToXXX overloads). Convert.ToDouble(Double) is the same, as well as Convert.ToSingle(Single), etc.

                        V Offline
                        V Offline
                        Vasudevan Deepak Kumar
                        wrote on last edited by
                        #14

                        Ron Beyer wrote:

                        Convert.ToInt32(Int32) does nothing

                        but to throw exceptions... Prefer Int32.TryParse instead.

                        Vasudevan Deepak Kumar Personal Homepage You can not step into the same river twice.

                        1 Reply Last reply
                        0
                        • A Argonia

                          By M$ logic the point of using that kind of "useful" methods is when you call Convert.ToInt32 with object, dynamic or var variable you will always get Int32 as a result. P.S. Today i found one more reason to dislike c#. Who the hell had the brilliant idea to make Tuples read only ? And why the elephant ?

                          Microsoft ... the only place where VARIANT_TRUE != true

                          R Offline
                          R Offline
                          robocodeboy
                          wrote on last edited by
                          #15

                          Take no offense, but you don't know what you're talking about, don't you? Mutable Tuples are called lists and/or classes. Having Tuples is great if you need to have a Dictionary which happens to need two keys: you don't need a custom class or some other custom solution, you just use the tuple as key. Mutable data means the hash will change, so you cannot. Read Immutable as hashable. It really makes a LOT of sense to have tuples. You just have to learn why they are there in the first place and you'll discover that the .NET Framework is one of the best around. And IMO the best, period. My 2 c: Convert is a fossil from 1.0/1.1 era. Never used it since generics went out.

                          A 1 Reply Last reply
                          0
                          • R robocodeboy

                            Take no offense, but you don't know what you're talking about, don't you? Mutable Tuples are called lists and/or classes. Having Tuples is great if you need to have a Dictionary which happens to need two keys: you don't need a custom class or some other custom solution, you just use the tuple as key. Mutable data means the hash will change, so you cannot. Read Immutable as hashable. It really makes a LOT of sense to have tuples. You just have to learn why they are there in the first place and you'll discover that the .NET Framework is one of the best around. And IMO the best, period. My 2 c: Convert is a fossil from 1.0/1.1 era. Never used it since generics went out.

                            A Offline
                            A Offline
                            Argonia
                            wrote on last edited by
                            #16

                            Actually i do, So basically i was looking for a way to make a vector with a pair of strings. After some google ing i found about list with Tuples. It was fun when i decided that this list must me modified. After few hours later i found about KeyValuePair. And btw everything what M$ sux including c#.

                            Microsoft ... the only place where VARIANT_TRUE != true

                            R 1 Reply Last reply
                            0
                            • A Argonia

                              Actually i do, So basically i was looking for a way to make a vector with a pair of strings. After some google ing i found about list with Tuples. It was fun when i decided that this list must me modified. After few hours later i found about KeyValuePair. And btw everything what M$ sux including c#.

                              Microsoft ... the only place where VARIANT_TRUE != true

                              R Offline
                              R Offline
                              robocodeboy
                              wrote on last edited by
                              #17

                              So you're telling me you needed a screwdriver, but you don't know what a screwdriver is. So you googled for it and picked up a drill. Then you found out that it didn't worked to do the job you need. And you blame Microsoft for this. I suggest you could read "The Pragmatic Programmer". Look, I'm not a MS fan boy, I think many libraries MS is pushing (or where pushing) are crap. But there is a core of consistency and completeness in the .NET framework that I never found out in other core libraries. And an elegance in C# that is unknown to Java. If you don't agree, tell me a better language to do the same kind of stuff you can do in C#. Currently, you seem to bash M$ just for the sake of your incompetency (literally: you're not competent regarding the job you do). I love Python (but it's library is light years lagging on naming conventions), I enjoy JavaScript (but you must know what works and what will break your programs), but the building of the .NET Framework is a major accomplishment.

                              R A 2 Replies Last reply
                              0
                              • R robocodeboy

                                So you're telling me you needed a screwdriver, but you don't know what a screwdriver is. So you googled for it and picked up a drill. Then you found out that it didn't worked to do the job you need. And you blame Microsoft for this. I suggest you could read "The Pragmatic Programmer". Look, I'm not a MS fan boy, I think many libraries MS is pushing (or where pushing) are crap. But there is a core of consistency and completeness in the .NET framework that I never found out in other core libraries. And an elegance in C# that is unknown to Java. If you don't agree, tell me a better language to do the same kind of stuff you can do in C#. Currently, you seem to bash M$ just for the sake of your incompetency (literally: you're not competent regarding the job you do). I love Python (but it's library is light years lagging on naming conventions), I enjoy JavaScript (but you must know what works and what will break your programs), but the building of the .NET Framework is a major accomplishment.

                                R Offline
                                R Offline
                                robocodeboy
                                wrote on last edited by
                                #18

                                Oh, and BTW KeyValuePair is not a vector. It's only a Dictionary item. You could use it to store two strings, but you probably would be better set implementing a class with two members, x and y.

                                A 1 Reply Last reply
                                0
                                • R robocodeboy

                                  So you're telling me you needed a screwdriver, but you don't know what a screwdriver is. So you googled for it and picked up a drill. Then you found out that it didn't worked to do the job you need. And you blame Microsoft for this. I suggest you could read "The Pragmatic Programmer". Look, I'm not a MS fan boy, I think many libraries MS is pushing (or where pushing) are crap. But there is a core of consistency and completeness in the .NET framework that I never found out in other core libraries. And an elegance in C# that is unknown to Java. If you don't agree, tell me a better language to do the same kind of stuff you can do in C#. Currently, you seem to bash M$ just for the sake of your incompetency (literally: you're not competent regarding the job you do). I love Python (but it's library is light years lagging on naming conventions), I enjoy JavaScript (but you must know what works and what will break your programs), but the building of the .NET Framework is a major accomplishment.

                                  A Offline
                                  A Offline
                                  Argonia
                                  wrote on last edited by
                                  #19

                                  robocodeboy wrote:

                                  And an elegance in C# that is unknown to Java.

                                  You are mistaken c# doesn't have any elegance. The elegance you see is only part of the beauties of c and c++. But ofcouse M$ decided that not all is needed and they decided to leave some things (templates, pointers, friend, and so on) and call it a new language because they added few libraries.

                                  Microsoft ... the only place where VARIANT_TRUE != true

                                  R 1 Reply Last reply
                                  0
                                  • R robocodeboy

                                    Oh, and BTW KeyValuePair is not a vector. It's only a Dictionary item. You could use it to store two strings, but you probably would be better set implementing a class with two members, x and y.

                                    A Offline
                                    A Offline
                                    Argonia
                                    wrote on last edited by
                                    #20

                                    My point was that i needed List> somesh*t = new List>(); for my vector. There is a Pair structure defined in System.Web.UI.Pair namespace. Who the hell does that. Define the same thing under different name in different namespace, oh, wait M$ Now you have 3 things for pair -Tuple - up to 7 items cuz you never know if the programmer can make 7 items list with Pair alone -KeyValuePair -Pair When the last 2 are basically the same.

                                    Microsoft ... the only place where VARIANT_TRUE != true

                                    R 1 Reply Last reply
                                    0
                                    • A Argonia

                                      robocodeboy wrote:

                                      And an elegance in C# that is unknown to Java.

                                      You are mistaken c# doesn't have any elegance. The elegance you see is only part of the beauties of c and c++. But ofcouse M$ decided that not all is needed and they decided to leave some things (templates, pointers, friend, and so on) and call it a new language because they added few libraries.

                                      Microsoft ... the only place where VARIANT_TRUE != true

                                      R Offline
                                      R Offline
                                      robocodeboy
                                      wrote on last edited by
                                      #21

                                      Yes, of course. C++ is so elegant that his grammar is undecidable, and no IDE in the world can give you a decent code completion. But maybe you're Klingon and you remember variable names by heart. C is elegant, yes. Just as an haiku. I completely enjoyed writing really quick, incomprehensible code that did simple stuff in clever ways, but designing UIs in C or C++ is an exercise in pain. Not mentioning organizing and building complex projects. C++ templates can do some clever stuff, but generics are pretty similar and less prone to breaking apart your build time. The only thing I miss in C# are mixins, but they are hard to manage even in C++. Pointers are in C# too, but you don't need to use them, usually. You can, but only if you have tight loops to be optimized. Friend is useless if you have internal members and friend assemblies. And private is considered more valuable than it is and not unit testable. Oh, and if "few libraries" means the single most extended library in existence yes, you are right. To one that makes this kind of statements, I can only suggest to (choose one in three): - RTFM and stop blaming others for what you get in the toolkit, instead of having to roll your own. - change language. Maybe the kind of stuff you're doing is not best suited for C# (I doubt, but...) - write your own language. You seem to be a world class language guru. Way better than that M$ scum. For sure you can tackle that task. A language is a tool, if you can't do stuff and blame the tool, either you change it or learn to use it. Blaming is easier (I remember people complaining about that stupid C compiler that was continually raising segmentation faults). Good luck!

                                      1 Reply Last reply
                                      0
                                      • A Argonia

                                        My point was that i needed List> somesh*t = new List>(); for my vector. There is a Pair structure defined in System.Web.UI.Pair namespace. Who the hell does that. Define the same thing under different name in different namespace, oh, wait M$ Now you have 3 things for pair -Tuple - up to 7 items cuz you never know if the programmer can make 7 items list with Pair alone -KeyValuePair -Pair When the last 2 are basically the same.

                                        Microsoft ... the only place where VARIANT_TRUE != true

                                        R Offline
                                        R Offline
                                        robocodeboy
                                        wrote on last edited by
                                        #22

                                        There is no point in what you're saying. There wasn't before and for sure there isn't in this. What do you have to do with that data? It's an x and y coordinates (like a vector) or it's simply two strings? Why an array string[2] is not ok? What you need to accomplish? And why aren't you creating a simple, 5 lines class to do that? With 2 properties x and y? Do I have to explain you the difference between those 3? It seems you cannot read the documentation... Don't blame the language, blame the programmer. Stop whining and read a book.

                                        A 1 Reply Last reply
                                        0
                                        • R robocodeboy

                                          There is no point in what you're saying. There wasn't before and for sure there isn't in this. What do you have to do with that data? It's an x and y coordinates (like a vector) or it's simply two strings? Why an array string[2] is not ok? What you need to accomplish? And why aren't you creating a simple, 5 lines class to do that? With 2 properties x and y? Do I have to explain you the difference between those 3? It seems you cannot read the documentation... Don't blame the language, blame the programmer. Stop whining and read a book.

                                          A Offline
                                          A Offline
                                          Argonia
                                          wrote on last edited by
                                          #23

                                          I was talking about this vector.

                                          Microsoft ... the only place where VARIANT_TRUE != true

                                          R 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