How do I convert a string to Int32?

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

    How do I convert a string to Int32?

    Doing something like this (cast) doesn't work.

    Y.value = (Int32)stringva riable;

    I get this message:
    Cannot convert type 'string' to 'int'

    Any idea how I can convert string to Int32?

    thxs
    Jeff Reed


  • Frank Oquendo

    #2
    Re: How do I convert a string to Int32?

    Jeff Reed wrote:
    [color=blue]
    > Any idea how I can convert string to Int32?[/color]

    int.Parse(strin g)

    There's also the Convert class or double.TryParse . I like TryParse
    because it won't throw an exception.

    --
    There are 10 kinds of people. Those who understand binary and those who
    don't.


    (Pull the pin to reply)


    Comment

    • Nivek Eroom

      #3
      Re: How do I convert a string to Int32?

      Hi to you,

      TryParse is a little bit slower. If you're sure that your string is a
      numeric (You can use RegularExpressi on) then i suggest you Int32.Parse or
      int.Parse (same thing)


      --
      Nivek Eroom

      Post message to group, everyones got benefits

      "Frank Oquendo" <frankopin@acad x.com> wrote in message
      news:%23YfU0GKq DHA.648@TK2MSFT NGP11.phx.gbl.. .[color=blue]
      > Jeff Reed wrote:
      >[color=green]
      > > Any idea how I can convert string to Int32?[/color]
      >
      > int.Parse(strin g)
      >
      > There's also the Convert class or double.TryParse . I like TryParse
      > because it won't throw an exception.
      >
      > --
      > There are 10 kinds of people. Those who understand binary and those who
      > don't.
      >
      > http://code.acadx.com
      > (Pull the pin to reply)
      >
      >[/color]


      Comment

      • Michael Culley

        #4
        Re: How do I convert a string to Int32?

        "Frank Oquendo" <frankopin@acad x.com> wrote in message news:#YfU0GKqDH A.648@TK2MSFTNG P11.phx.gbl...[color=blue]
        > There's also the Convert class or double.TryParse .[/color]

        You might still get an error converting the double to an int. It would be fairly easy to write a TryParse function for int, or
        create a function that does the conversion and returns 0 if there is an error (or a default value).

        --
        Michael Culley


        Comment

        • Frank Oquendo

          #5
          Re: How do I convert a string to Int32?

          Michael Culley wrote:
          [color=blue]
          > You might still get an error converting the double to an int. It
          > would be fairly easy to write a TryParse function for int, or create
          > a function that does the conversion and returns 0 if there is an
          > error (or a default value).[/color]

          TryParse enver throws an exeption. Instead it will return false and set
          the out parameter to 0. You can also specify NumberStyles.In teger as an
          argument. That will help avoid any casting errors. I use TryParse all
          the time and have never had a problem with using to convert strings to
          ints.

          --
          There are 10 kinds of people. Those who understand binary and those who
          don't.


          (Pull the pin to reply)


          Comment

          • Michael Culley

            #6
            Re: How do I convert a string to Int32?

            I didn't know about the second parameter. However, if you create your own function you don't require a cast or to type the second
            param. Most the time in my app I just do Gen.ToInt32(x) where Gen is a class I use for common functions.

            --
            Michael Culley


            "Frank Oquendo" <frankopin@acad x.com> wrote in message news:uoo15HMqDH A.2636@tk2msftn gp13.phx.gbl...[color=blue]
            > Michael Culley wrote:
            >[color=green]
            > > You might still get an error converting the double to an int. It
            > > would be fairly easy to write a TryParse function for int, or create
            > > a function that does the conversion and returns 0 if there is an
            > > error (or a default value).[/color]
            >
            > TryParse enver throws an exeption. Instead it will return false and set
            > the out parameter to 0. You can also specify NumberStyles.In teger as an
            > argument. That will help avoid any casting errors. I use TryParse all
            > the time and have never had a problem with using to convert strings to
            > ints.
            >
            > --
            > There are 10 kinds of people. Those who understand binary and those who
            > don't.
            >
            > http://code.acadx.com
            > (Pull the pin to reply)
            >
            >[/color]


            Comment

            • Frank Oquendo

              #7
              Re: How do I convert a string to Int32?

              Michael Culley wrote:[color=blue]
              > I didn't know about the second parameter.[/color]

              There's more than that as well. Another parameter is NumberFormatInf o
              object. I normally pass
              System.Globaliz ation.CultureIn fo.CurrentCultu re.NumberFormat . Between
              that and the NumberStyles parameter, you can parse all manner of strings
              such as 10000, 10,000 or even $10,000 all with a single line of code.
              [color=blue]
              > However, if you create your
              > own function you don't require a cast or to type the second param.[/color]

              Yep, a simple wrapper does come in handy if you don't want to perform a
              cast from double to int.

              --
              There are 10 kinds of people. Those who understand binary and those who
              don't.


              (Pull the pin to reply)


              Comment

              Working...