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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. numeric conversion for if check

numeric conversion for if check

Scheduled Pinned Locked Moved C#
help
11 Posts 4 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
    Mairaaj Khan
    wrote on last edited by
    #1

    Hi, i want to check the existence of an integer value in the textbox control with the following code: if (Convert.ToInt16(tbStockTypeId.Text) <= 0) works fine when an intger value is entered. but when i enter 'abc' it raises FormatException "Input string was not in a correct format." Need help for its correction. With Thanx

    J V R 3 Replies Last reply
    0
    • M Mairaaj Khan

      Hi, i want to check the existence of an integer value in the textbox control with the following code: if (Convert.ToInt16(tbStockTypeId.Text) <= 0) works fine when an intger value is entered. but when i enter 'abc' it raises FormatException "Input string was not in a correct format." Need help for its correction. With Thanx

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      First check if the Text value is numeric before trying to convert it. This can be done with a RegEx, or Char.IsNumeric Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

      M 1 Reply Last reply
      0
      • M Mairaaj Khan

        Hi, i want to check the existence of an integer value in the textbox control with the following code: if (Convert.ToInt16(tbStockTypeId.Text) <= 0) works fine when an intger value is entered. but when i enter 'abc' it raises FormatException "Input string was not in a correct format." Need help for its correction. With Thanx

        V Offline
        V Offline
        V 0
        wrote on last edited by
        #3

        try{ int stocktype = Convert.ToInt16(tbStockTypeId.Text);   if(stocktype <= 0) {     //do stuff   } } catch(FormatException fexc){   Messagebox(tbStockTypeId.Text + "Is not a number"); } Coulda, woulda, shoulda doesn't matter if you don't. [MODIFIED]In this 32 bit world, use ToInt32 unless you have little memory.[/MODIFIED] :beer:
        :jig: -- modified at 10:53 Friday 26th May, 2006

        J M 2 Replies Last reply
        0
        • V V 0

          try{ int stocktype = Convert.ToInt16(tbStockTypeId.Text);   if(stocktype <= 0) {     //do stuff   } } catch(FormatException fexc){   Messagebox(tbStockTypeId.Text + "Is not a number"); } Coulda, woulda, shoulda doesn't matter if you don't. [MODIFIED]In this 32 bit world, use ToInt32 unless you have little memory.[/MODIFIED] :beer:
          :jig: -- modified at 10:53 Friday 26th May, 2006

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          Although that would work absolutely fine, its generally considered bad practice to control program flow with exceptions. Exceptions for exceptional behaviour - the user making an invalid entry is not exceptional, it is expected! Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

          V 1 Reply Last reply
          0
          • J J4amieC

            Although that would work absolutely fine, its generally considered bad practice to control program flow with exceptions. Exceptions for exceptional behaviour - the user making an invalid entry is not exceptional, it is expected! Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

            V Offline
            V Offline
            V 0
            wrote on last edited by
            #5

            J4amieC wrote:

            the user making an invalid entry is not exceptional, it is expected

            He he , I agree on this, but I don't understand why using try/catch is bad practice? Coulda, woulda, shoulda doesn't matter if you don't. :beer:
                :jig:

            J 1 Reply Last reply
            0
            • V V 0

              J4amieC wrote:

              the user making an invalid entry is not exceptional, it is expected

              He he , I agree on this, but I don't understand why using try/catch is bad practice? Coulda, woulda, shoulda doesn't matter if you don't. :beer:
                  :jig:

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              I guess because throwing exceptions is just more resource intensive, its a lot quicker to first check if the value is valid. Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

              V 1 Reply Last reply
              0
              • J J4amieC

                I guess because throwing exceptions is just more resource intensive, its a lot quicker to first check if the value is valid. Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

                V Offline
                V Offline
                V 0
                wrote on last edited by
                #7

                mja, sounds reasonable. tnx. Coulda, woulda, shoulda doesn't matter if you don't. :beer:
                    :jig:

                1 Reply Last reply
                0
                • M Mairaaj Khan

                  Hi, i want to check the existence of an integer value in the textbox control with the following code: if (Convert.ToInt16(tbStockTypeId.Text) <= 0) works fine when an intger value is entered. but when i enter 'abc' it raises FormatException "Input string was not in a correct format." Need help for its correction. With Thanx

                  R Offline
                  R Offline
                  Robin Panther
                  wrote on last edited by
                  #8

                  If you are using .NET 2.0 - why not use Int32.TryParse()? short outint; if (!Int16.TryParse(tbStockTypeId.Text, out outint)) { throw new ArgumentException("Argument is not integer", "tbStockTypeId"); } //use outint ____________________________________________ Robin Panther http://www.robinland.com

                  M 1 Reply Last reply
                  0
                  • J J4amieC

                    First check if the Text value is numeric before trying to convert it. This can be done with a RegEx, or Char.IsNumeric Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

                    M Offline
                    M Offline
                    Mairaaj Khan
                    wrote on last edited by
                    #9

                    thanks for reply! J4amieC Char.IsNumeric is fine for a character, while i'm working with textbox string. uroojkhan

                    1 Reply Last reply
                    0
                    • V V 0

                      try{ int stocktype = Convert.ToInt16(tbStockTypeId.Text);   if(stocktype <= 0) {     //do stuff   } } catch(FormatException fexc){   Messagebox(tbStockTypeId.Text + "Is not a number"); } Coulda, woulda, shoulda doesn't matter if you don't. [MODIFIED]In this 32 bit world, use ToInt32 unless you have little memory.[/MODIFIED] :beer:
                      :jig: -- modified at 10:53 Friday 26th May, 2006

                      M Offline
                      M Offline
                      Mairaaj Khan
                      wrote on last edited by
                      #10

                      thanks V! about Exceptions agree with J4amieC. Do remember (for Int16 and Int32) What u required is more important than what u have! Regards, uroojkhan

                      1 Reply Last reply
                      0
                      • R Robin Panther

                        If you are using .NET 2.0 - why not use Int32.TryParse()? short outint; if (!Int16.TryParse(tbStockTypeId.Text, out outint)) { throw new ArgumentException("Argument is not integer", "tbStockTypeId"); } //use outint ____________________________________________ Robin Panther http://www.robinland.com

                        M Offline
                        M Offline
                        Mairaaj Khan
                        wrote on last edited by
                        #11

                        with a lot of thanks Robin Panther! its solved my problem. Do remember (for Int16 and Int32) What u required is more important than what u have! Regards, uroojkhan

                        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