convert string to int

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • djozy

    #1

    convert string to int

    Please, how can I convert selected item from dropdown list
    to integer?
    Thank you
  • Miha Markic

    #2
    Re: convert string to int

    Hi,

    Convert.ToInt32

    --
    Miha Markic - RightHand .NET consulting & software development
    miha at rthand com

    "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
    news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..[color=blue]
    > Please, how can I convert selected item from dropdown list
    > to integer?
    > Thank you[/color]


    Comment

    • Daniel Bass

      #3
      Re: convert string to int

      int myInt = System.Convert. ToInt32 ( name of your item )
      Dan.

      "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
      news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..
      Please, how can I convert selected item from dropdown list
      to integer?
      Thank you


      Comment

      • Morten Wennevik

        #4
        Re: convert string to int

        Are you thinking about

        int selected = comboBox1.Selec tedIndex;

        Or just how to convert a string to integer?

        int number = Convert.ToInt32 (text);
        int number = Int32.Parse(tex t);

        --
        Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

        Comment

        • Ignacio Machin \( .NET/ C#  MVP \)

          #5
          Re: convert string to int

          Hi,

          If you are sure that the values of the dropdown are numbers you can do
          like:
          int val = Convert.ToInt32 ( DropDownControl . SelectedValue );

          If you are not then you can wrap the above line in a try/catch block.


          Cheers,

          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation

          "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
          news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..[color=blue]
          > Please, how can I convert selected item from dropdown list
          > to integer?
          > Thank you[/color]


          Comment

          • William Stacey

            #6
            Re: convert string to int

            Is there a CanConvert like method that returns a bool instead of an
            exception. The problem with wrapping in try is the wait time for the
            exception.

            --
            William Stacey, MVP

            "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
            in message news:#2XwdHyvDH A.2136@TK2MSFTN GP10.phx.gbl...[color=blue]
            > Hi,
            >
            > If you are sure that the values of the dropdown are numbers you can do
            > like:
            > int val = Convert.ToInt32 ( DropDownControl . SelectedValue );
            >
            > If you are not then you can wrap the above line in a try/catch block.
            >
            >
            > Cheers,
            >
            > --
            > Ignacio Machin,
            > ignacio.machin AT dot.state.fl.us
            > Florida Department Of Transportation
            >
            > "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
            > news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..[color=green]
            > > Please, how can I convert selected item from dropdown list
            > > to integer?
            > > Thank you[/color]
            >
            >[/color]


            Comment

            • Ignacio Machin \( .NET/ C#  MVP \)

              #7
              Re: convert string to int

              Hi William,

              AFAIK there is not such a method in the framework, what you can do is
              include the VB.NET DLL which define a IsNumeric function.


              Cheers,

              --
              Ignacio Machin,
              ignacio.machin AT dot.state.fl.us
              Florida Department Of Transportation

              "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
              news:%23jwCgBzv DHA.1740@TK2MSF TNGP12.phx.gbl. ..[color=blue]
              > Is there a CanConvert like method that returns a bool instead of an
              > exception. The problem with wrapping in try is the wait time for the
              > exception.
              >
              > --
              > William Stacey, MVP
              >
              > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >[/color]
              wrote[color=blue]
              > in message news:#2XwdHyvDH A.2136@TK2MSFTN GP10.phx.gbl...[color=green]
              > > Hi,
              > >
              > > If you are sure that the values of the dropdown are numbers you can do
              > > like:
              > > int val = Convert.ToInt32 ( DropDownControl . SelectedValue );
              > >
              > > If you are not then you can wrap the above line in a try/catch block.
              > >
              > >
              > > Cheers,
              > >
              > > --
              > > Ignacio Machin,
              > > ignacio.machin AT dot.state.fl.us
              > > Florida Department Of Transportation
              > >
              > > "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
              > > news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..[color=darkred]
              > > > Please, how can I convert selected item from dropdown list
              > > > to integer?
              > > > Thank you[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Miha Markic

                #8
                Re: convert string to int

                Hi William,

                You might also use Regex as it should be fairly simple to check for the
                integers.

                --
                Miha Markic - DXSquad/RightHand .NET consulting & software development
                miha at rthand com


                "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
                news:%23jwCgBzv DHA.1740@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                > Is there a CanConvert like method that returns a bool instead of an
                > exception. The problem with wrapping in try is the wait time for the
                > exception.
                >
                > --
                > William Stacey, MVP
                >
                > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >[/color]
                wrote[color=blue]
                > in message news:#2XwdHyvDH A.2136@TK2MSFTN GP10.phx.gbl...[color=green]
                > > Hi,
                > >
                > > If you are sure that the values of the dropdown are numbers you can do
                > > like:
                > > int val = Convert.ToInt32 ( DropDownControl . SelectedValue );
                > >
                > > If you are not then you can wrap the above line in a try/catch block.
                > >
                > >
                > > Cheers,
                > >
                > > --
                > > Ignacio Machin,
                > > ignacio.machin AT dot.state.fl.us
                > > Florida Department Of Transportation
                > >
                > > "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
                > > news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..[color=darkred]
                > > > Please, how can I convert selected item from dropdown list
                > > > to integer?
                > > > Thank you[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Reinhold Schalk

                  #9
                  Re: convert string to int

                  Hi Ignacio,
                  one problem with VB IsNumeric: does not explicitly check for integers.
                  that means IsNumeric() does also return true, if your string is a double(or
                  float). that's not always what yout want.

                  Reinhold
                  (Sorry for bad english)

                  "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
                  schrieb im Newsbeitrag news:OpRgoJzvDH A.536@tk2msftng p13.phx.gbl...[color=blue]
                  > Hi William,
                  >
                  > AFAIK there is not such a method in the framework, what you can do is
                  > include the VB.NET DLL which define a IsNumeric function.
                  >
                  >
                  > Cheers,
                  >
                  > --
                  > Ignacio Machin,
                  > ignacio.machin AT dot.state.fl.us
                  > Florida Department Of Transportation
                  >
                  > "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
                  > news:%23jwCgBzv DHA.1740@TK2MSF TNGP12.phx.gbl. ..[color=green]
                  > > Is there a CanConvert like method that returns a bool instead of an
                  > > exception. The problem with wrapping in try is the wait time for the
                  > > exception.
                  > >
                  > > --
                  > > William Stacey, MVP
                  > >
                  > > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >[/color]
                  > wrote[color=green]
                  > > in message news:#2XwdHyvDH A.2136@TK2MSFTN GP10.phx.gbl...[color=darkred]
                  > > > Hi,
                  > > >
                  > > > If you are sure that the values of the dropdown are numbers you can[/color][/color][/color]
                  do[color=blue][color=green][color=darkred]
                  > > > like:
                  > > > int val = Convert.ToInt32 ( DropDownControl . SelectedValue );
                  > > >
                  > > > If you are not then you can wrap the above line in a try/catch block.
                  > > >
                  > > >
                  > > > Cheers,
                  > > >
                  > > > --
                  > > > Ignacio Machin,
                  > > > ignacio.machin AT dot.state.fl.us
                  > > > Florida Department Of Transportation
                  > > >
                  > > > "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
                  > > > news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..
                  > > > > Please, how can I convert selected item from dropdown list
                  > > > > to integer?
                  > > > > Thank you
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • Ignacio Machin \( .NET/ C#  MVP \)

                    #10
                    Re: convert string to int

                    Hi,

                    Using this you will also need to check for overflow :)

                    Cheers,

                    --
                    Ignacio Machin,
                    ignacio.machin AT dot.state.fl.us
                    Florida Department Of Transportation

                    "Miha Markic" <miha at rthand com> wrote in message
                    news:er5acMzvDH A.2492@TK2MSFTN GP12.phx.gbl...[color=blue]
                    > Hi William,
                    >
                    > You might also use Regex as it should be fairly simple to check for the
                    > integers.
                    >
                    > --
                    > Miha Markic - DXSquad/RightHand .NET consulting & software development
                    > miha at rthand com
                    >
                    >
                    > "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
                    > news:%23jwCgBzv DHA.1740@TK2MSF TNGP12.phx.gbl. ..[color=green]
                    > > Is there a CanConvert like method that returns a bool instead of an
                    > > exception. The problem with wrapping in try is the wait time for the
                    > > exception.
                    > >
                    > > --
                    > > William Stacey, MVP
                    > >
                    > > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >[/color]
                    > wrote[color=green]
                    > > in message news:#2XwdHyvDH A.2136@TK2MSFTN GP10.phx.gbl...[color=darkred]
                    > > > Hi,
                    > > >
                    > > > If you are sure that the values of the dropdown are numbers you can[/color][/color][/color]
                    do[color=blue][color=green][color=darkred]
                    > > > like:
                    > > > int val = Convert.ToInt32 ( DropDownControl . SelectedValue );
                    > > >
                    > > > If you are not then you can wrap the above line in a try/catch block.
                    > > >
                    > > >
                    > > > Cheers,
                    > > >
                    > > > --
                    > > > Ignacio Machin,
                    > > > ignacio.machin AT dot.state.fl.us
                    > > > Florida Department Of Transportation
                    > > >
                    > > > "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
                    > > > news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..
                    > > > > Please, how can I convert selected item from dropdown list
                    > > > > to integer?
                    > > > > Thank you
                    > > >
                    > > >[/color]
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    • Daniel Bass

                      #11
                      Re: convert string to int

                      You're slightly missing the point then if the only check you have is an
                      exception.

                      Exceptions are meant to catch errors that are unforseen, and while .Net (in
                      particular) c# and VB.Net seem to use more exceptions for returning errors
                      (rather than a bool or hresult), good code (i.e. code that's past the beta
                      stage of testing and in the RC) should never hit an exception.

                      wrapping code in try/catch is essential for reporting to you when things
                      don't go as you'd expect... if you leave the door open (ie an alpha numeric
                      text box) for user errors then there should be tighter restrictions made on
                      what the user can do to prevent this from happening.

                      If you hit a situation where you're trying to access an object for a
                      conversion to int and it can't be a converted to an int, then your code
                      needs reviewing, and perhaps tighter controls implemented, but the exception
                      should still be there to catch anything that goes wrong.

                      In this case, the initial enquiry has little weight since an exception is
                      more of a debugging facility when the unexpected errors occur, rather than
                      handling everything that goes wrong that could go wrong, because it's not
                      been catered for.

                      Hope that helps.
                      Dan.

                      "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
                      news:%23jwCgBzv DHA.1740@TK2MSF TNGP12.phx.gbl. ..
                      Is there a CanConvert like method that returns a bool instead of an
                      exception. The problem with wrapping in try is the wait time for the
                      exception.

                      --
                      William Stacey, MVP

                      "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
                      in message news:#2XwdHyvDH A.2136@TK2MSFTN GP10.phx.gbl...[color=blue]
                      > Hi,
                      >
                      > If you are sure that the values of the dropdown are numbers you can do
                      > like:
                      > int val = Convert.ToInt32 ( DropDownControl . SelectedValue );
                      >
                      > If you are not then you can wrap the above line in a try/catch block.
                      >
                      >
                      > Cheers,
                      >
                      > --
                      > Ignacio Machin,
                      > ignacio.machin AT dot.state.fl.us
                      > Florida Department Of Transportation
                      >
                      > "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
                      > news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..[color=green]
                      > > Please, how can I convert selected item from dropdown list
                      > > to integer?
                      > > Thank you[/color]
                      >
                      >[/color]



                      Comment

                      • Daniel Bass

                        #12
                        Re: convert string to int

                        the regex is probably one of the more efficient ways of confirming the
                        number...



                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: convert string to int

                          <"Daniel Bass" <I'm really @ sick of spam>> wrote:[color=blue]
                          > the regex is probably one of the more efficient ways of confirming the
                          > number...[/color]

                          Not really. Going through the characters in the string and checking for
                          digits is *far* more efficient.

                          Search in groups.google.c om for a thread called "Checking if a string
                          can be converted to Int32" in microsoft.publi c.dotnet.framew ork. The
                          results from a bit of benchmarking were (in times for checking 9
                          strings 100,000 times each):

                          Just using an exception: 1m 16s
                          A hand-crafted checking routine: 0.7s
                          Double.TryParse (for first pass removals): 41s
                          Regular expression: 43s
                          The VB "IsNumeric" method: 1m 7s

                          Regular expressions are incredibly powerful things, but for simple
                          tests like this, hand-crafted code is almost always going to be more
                          efficient.

                          --
                          Jon Skeet - <skeet@pobox.co m>
                          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                          If replying to the group, please do not mail me too

                          Comment

                          • William Stacey

                            #14
                            Re: convert string to int

                            Thanks. That was point of checking for isnumeric first, so you don't have
                            to hit the exception.

                            --
                            William Stacey, MVP

                            "Daniel Bass" <I'm really @ sick of spam> wrote in message
                            news:OtPRIazvDH A.1088@tk2msftn gp13.phx.gbl...[color=blue]
                            > You're slightly missing the point then if the only check you have is an
                            > exception.[/color]
                            ....


                            Comment

                            • William Stacey

                              #15
                              Re: convert string to int

                              Thats good info. I would think that after you got that far, converting to a
                              number yourself could be faster then calling int.parse() or the like.

                              --
                              William Stacey, MVP

                              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                              news:MPG.1a4159 b176e0ec9989bf7 @msnews.microso ft.com...[color=blue]
                              > <"Daniel Bass" <I'm really @ sick of spam>> wrote:[color=green]
                              > > the regex is probably one of the more efficient ways of confirming the
                              > > number...[/color]
                              >
                              > Not really. Going through the characters in the string and checking for
                              > digits is *far* more efficient.
                              >
                              > Search in groups.google.c om for a thread called "Checking if a string
                              > can be converted to Int32" in microsoft.publi c.dotnet.framew ork. The
                              > results from a bit of benchmarking were (in times for checking 9
                              > strings 100,000 times each):
                              >
                              > Just using an exception: 1m 16s
                              > A hand-crafted checking routine: 0.7s
                              > Double.TryParse (for first pass removals): 41s
                              > Regular expression: 43s
                              > The VB "IsNumeric" method: 1m 7s
                              >
                              > Regular expressions are incredibly powerful things, but for simple
                              > tests like this, hand-crafted code is almost always going to be more
                              > efficient.
                              >
                              > --
                              > Jon Skeet - <skeet@pobox.co m>
                              > http://www.pobox.com/~skeet
                              > If replying to the group, please do not mail me too[/color]


                              Comment

                              Working...