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. convert string to Int

convert string to Int

Scheduled Pinned Locked Moved C#
question
10 Posts 8 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.
  • M Offline
    M Offline
    minniemooo
    wrote on last edited by
    #1

    How do I convert a string textbox to int?

    L G V P C 6 Replies Last reply
    0
    • M minniemooo

      How do I convert a string textbox to int?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      int.Parse, int.TryParse or Convert.ToInt32

      1 Reply Last reply
      0
      • M minniemooo

        How do I convert a string textbox to int?

        G Offline
        G Offline
        GuyThiebaut
        wrote on last edited by
        #3

        Convert.ToInt16(SomeTextBox.Text)

        You always pass failure on the way to success.
        1 Reply Last reply
        0
        • M minniemooo

          How do I convert a string textbox to int?

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

          Convert.ToInt16() Convert.ToInt32() Convert.ToInt64()

          Vasudevan Deepak Kumar Personal Homepage
          Tech Gossips
          A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson

          1 Reply Last reply
          0
          • M minniemooo

            How do I convert a string textbox to int?

            P Offline
            P Offline
            Pankaj Joshi
            wrote on last edited by
            #5

            I don't say all of the answer you got above are wrong but they may be create problem (Error) If any user input any text in the textbox I.e. instead of numbers any other input will throw an error. So you have to make your own function...I also done this please use public static int ToInt(object obj) { string s = ToStr(obj); return (s.Length > 0 ? int.Parse(s) : 0); }

            Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

            C 1 Reply Last reply
            0
            • M minniemooo

              How do I convert a string textbox to int?

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

              int.TryParse

              Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

              1 Reply Last reply
              0
              • P Pankaj Joshi

                I don't say all of the answer you got above are wrong but they may be create problem (Error) If any user input any text in the textbox I.e. instead of numbers any other input will throw an error. So you have to make your own function...I also done this please use public static int ToInt(object obj) { string s = ToStr(obj); return (s.Length > 0 ? int.Parse(s) : 0); }

                Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

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

                I don't see how this is going to work better than int.TryParse, which was suggested above.

                Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                P 1 Reply Last reply
                0
                • C Christian Graus

                  I don't see how this is going to work better than int.TryParse, which was suggested above.

                  Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                  P Offline
                  P Offline
                  Pankaj Joshi
                  wrote on last edited by
                  #8

                  May be this is not a good solution but it won't give the error in the case of non numeric value is entered by user.

                  Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

                  M 1 Reply Last reply
                  0
                  • P Pankaj Joshi

                    May be this is not a good solution but it won't give the error in the case of non numeric value is entered by user.

                    Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

                    M Offline
                    M Offline
                    moon_stick
                    wrote on last edited by
                    #9

                    TryParse returns a boolean value to check whether or not the conversion was successful.

                    It definitely isn't definatley

                    1 Reply Last reply
                    0
                    • M minniemooo

                      How do I convert a string textbox to int?

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

                      Consider a NumericUpDown instead.

                      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