Local Settings and NumberFormat issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chad Z. Hower aka Kudzu

    #16
    Re: Local Settings and NumberFormat issues

    ru <ur@sx3all.be > wrote in
    news:10l8b1lc2u j5b0t6qvv9i2ddo v1rhuau81@4ax.c om:[color=blue]
    > I'm not aware of bypassing the system routines for parsing - how does
    > one do that :) Can you give me an example?[/color]

    Developers often write their own parsing routines, especially on non .NET platforms where the
    frameworks may not support it, or worse yet the frameworks rely on configurations from the
    developer and ignore the system settings.
    [color=blue]
    > int100 = CInt(txt100.Tex t)
    >
    > int100 is an integer
    > txt100 is a textbox displaying an integer variable: there's no decimal
    > separator involved, so I don't know why this is raising an error.[/color]

    Im more of a C# person than a VB person although before Delphi I used to do a lot of VB and
    even wrote for many magizines. CInt I know is a type of VB.NET cast but under the hood I dont
    remember what its C# equivalent is, IIRC its more equivalent to an implicit cast in VB.NET than a
    parse. Generally I would recommend against these and prefer the parse methods.

    Does it have a thousands separator in it possibly?
    [color=blue]
    > Changing the code to:
    > int100 = Integer.Parse(t xt100History.Te xt,
    > NumberStyles.Al lowDecimalPoint )
    > cleared the error.[/color]

    Does just Parse(txt100His tory.Text) work for you or must you specify the second argument? What
    is the exact string value of txt100Histor.Te xt when the exception is thrown?
    [color=blue]
    > Another thing I discovered was that the IsNumeric function also didn't
    > work properly. The following code would always return false, no matter
    > what key was pressed:
    >
    > Private Sub txtScore_TextCh anged(ByVal sender As Object, ByVal e As
    > System.EventArg s) Handles txtScore.TextCh anged
    > If IsNumeric(txtSc ore.Text) = False Then
    > txtScore.Text = ""
    > End If
    > 'End Sub[/color]

    Isnt IsNumeric one of the old VB comptability routines? Generally speaking you should avoid
    using these in new code and rely on FCL functionality instead. And is it possible that IsNumeric
    only checks the first character in the string? I seem to remember something about this from the
    long ago VB days.
    [color=blue]
    > I now notice that only parsing of variables from the form itself,
    > displayed by labels or textboxes, is causing troubles. I just tested a
    > conversion from an integer declared in code to a double to a string
    > without errors.[/color]

    Quite possibly because when you are dispalying it, regional settings are formatting it. Then when
    you are rereading it, its formatted.


    --
    Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
    "Programmin g is an art form that fights back"

    Empower ASP.NET with IntraWeb

    Comment

    • ru

      #17
      Re: Local Settings and NumberFormat issues

      On Sat, 18 Jun 2005 21:53:57 +0300, "Chad Z. Hower aka Kudzu"
      <cpub@hower.org > wrote:
      [color=blue]
      >ru <ur@sx3all.be > wrote in
      >news:10l8b1lc2 uj5b0t6qvv9i2dd ov1rhuau81@4ax. com:[color=green]
      >> I'm not aware of bypassing the system routines for parsing - how does
      >> one do that :) Can you give me an example?[/color]
      >
      >Developers often write their own parsing routines, especially on non .NET platforms where the
      >frameworks may not support it, or worse yet the frameworks rely on configurations from the
      >developer and ignore the system settings.
      >[color=green]
      >> int100 = CInt(txt100.Tex t)
      >>
      >> int100 is an integer
      >> txt100 is a textbox displaying an integer variable: there's no decimal
      >> separator involved, so I don't know why this is raising an error.[/color]
      >
      >Im more of a C# person than a VB person although before Delphi I used to do a lot of VB and
      >even wrote for many magizines. CInt I know is a type of VB.NET cast but under the hood I dont
      >remember what its C# equivalent is, IIRC its more equivalent to an implicit cast in VB.NET than a
      >parse. Generally I would recommend against these and prefer the parse methods.
      >
      >Does it have a thousands separator in it possibly?
      >[color=green]
      >> Changing the code to:
      >> int100 = Integer.Parse(t xt100History.Te xt,
      >> NumberStyles.Al lowDecimalPoint )
      >> cleared the error.[/color]
      >
      >Does just Parse(txt100His tory.Text) work for you or must you specify the second argument? What
      >is the exact string value of txt100Histor.Te xt when the exception is thrown?[/color]

      Wow, removing the second argument works like a charm. I use CInt and
      CDbl quite a lot, so I guess I'll have to change my habits.
      [color=blue][color=green]
      >> Another thing I discovered was that the IsNumeric function also didn't
      >> work properly. The following code would always return false, no matter
      >> what key was pressed:
      >>
      >> Private Sub txtScore_TextCh anged(ByVal sender As Object, ByVal e As
      >> System.EventArg s) Handles txtScore.TextCh anged
      >> If IsNumeric(txtSc ore.Text) = False Then
      >> txtScore.Text = ""
      >> End If
      >> 'End Sub[/color]
      >
      >Isnt IsNumeric one of the old VB comptability routines? Generally speaking you should avoid
      >using these in new code and rely on FCL functionality instead. And is it possible that IsNumeric
      >only checks the first character in the string? I seem to remember something about this from the
      >long ago VB days.[/color]

      I've changed the IsNumeric function to the following code:
      Private Sub txtScore_KeyPre ss(ByVal sender As Object, ByVal e As
      System.Windows. Forms.KeyPressE ventArgs) Handles txtScore.KeyPre ss
      If e.KeyChar < "0" Or e.KeyChar > "9" Then
      If (e.KeyChar = Chr(&H8)) Or (e.KeyChar = "Keys.Delet e")
      Then
      Else
      e.Handled = True
      End If
      End If
      End Sub

      This works, and is also more accurate in handling user input.
      [color=blue][color=green]
      >> I now notice that only parsing of variables from the form itself,
      >> displayed by labels or textboxes, is causing troubles. I just tested a
      >> conversion from an integer declared in code to a double to a string
      >> without errors.[/color]
      >
      >Quite possibly because when you are dispalying it, regional settings are formatting it. Then when
      >you are rereading it, its formatted.[/color]

      Thanks for your insight, these errors were really getting on my
      nerves. I shall lose the old VB code where I can. And to think I've
      never coded in classic VB, I can only imagine what oldtimers must go
      through.

      ru

      Comment

      • Chad Z. Hower aka Kudzu

        #18
        Re: Local Settings and NumberFormat issues

        ru <ur@sx3all.be > wrote in
        news:j54ab1psol ku95co5ihd72g24 r2q9jujj2@4ax.c om:[color=blue][color=green]
        >>Does just Parse(txt100His tory.Text) work for you or must you specify
        >>the second argument? What is the exact string value of
        >>txt100Histor. Text when the exception is thrown?[/color]
        >
        > Wow, removing the second argument works like a charm. I use CInt and
        > CDbl quite a lot, so I guess I'll have to change my habits.[/color]

        Yep, I thought that would do it. Definitely you should be using Parse methods instead of the cast
        variants.
        [color=blue][color=green]
        >>Quite possibly because when you are dispalying it, regional settings
        >>are formatting it. Then when you are rereading it, its formatted.[/color]
        >
        > Thanks for your insight, these errors were really getting on my
        > nerves. I shall lose the old VB code where I can. And to think I've
        > never coded in classic VB, I can only imagine what oldtimers must go
        > through.[/color]

        Its a big change for VB developers. But regional settings can be troubling too. I just know them very
        well because Ive lived in several parts of the world and travel quite a bit. I have an English-
        Russian-Greek-Arabic machine right now with Canadian regional settings. :)


        --
        Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
        "Programmin g is an art form that fights back"

        Get your ASP.NET in gear with IntraWeb!

        Comment

        • Cor Ligthert

          #19
          Re: Local Settings and NumberFormat issues

          Ru,

          I find the CInt an extremly strong instrucion.

          Can you try this.
          Set your decimal point to comma.
          Have a look if it is done for everything
          Create a new form
          Drag a textbox on the form
          Click on the form and set in than in the loadevent

          dim whatever as integer = Cint("1,00")

          For me the most compatible in system is this one
          System.Convert. ToInt32("1,00")

          However Cint should do it and is less typing.

          I am interested in the result.

          Cor



          Comment

          • Chad Z. Hower aka Kudzu

            #20
            Re: Local Settings and NumberFormat issues

            "Cor Ligthert" <notmyfirstname @planet.nl> wrote in news:#MzepEMdFH A.2588
            @TK2MSFTNGP15.p hx.gbl:[color=blue]
            > For me the most compatible in system is this one
            > System.Convert. ToInt32("1,00")[/color]

            Thats the same as Int32.Parse AFAIk, just a bit wordier. :)



            --
            Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
            "Programmin g is an art form that fights back"

            Make your ASP.NET applications run faster

            Comment

            • Cor Ligthert

              #21
              Re: Local Settings and NumberFormat issues

              > "Cor Ligthert" <notmyfirstname @planet.nl> wrote in news:#MzepEMdFH A.2588[color=blue]
              > @TK2MSFTNGP15.p hx.gbl:[color=green]
              >> For me the most compatible in system is this one
              >> System.Convert. ToInt32("1,00")[/color]
              >
              > Thats the same as Int32.Parse AFAIk, just a bit wordier. :)
              >[/color]
              I did not check it in Ildas, however I assume you are right, but because it
              is wordier is it in my opinion a little bit more compatible.

              :-)

              Cor


              Comment

              • Herfried K. Wagner [MVP]

                #22
                Re: Local Settings and NumberFormat issues

                "Chad Z. Hower aka Kudzu" <cpub@hower.org > schrieb:[color=blue][color=green]
                >> For me the most compatible in system is this one
                >> System.Convert. ToInt32("1,00")[/color]
                >
                > Thats the same as Int32.Parse AFAIk, just a bit wordier. :)[/color]

                No, it's not the same. 'Convert.ToInt3 2' will return 0 if 'Nothing' (a null
                reference) is passed to it, 'Int32.Parse' will throw an exception.

                --
                M S Herfried K. Wagner
                M V P <URL:http://dotnet.mvps.org/>
                V B <URL:http://classicvb.org/petition/>

                Comment

                • Chad Z. Hower aka Kudzu

                  #23
                  Re: Local Settings and NumberFormat issues

                  "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in
                  news:eFwACZPdFH A.220@TK2MSFTNG P12.phx.gbl:[color=blue]
                  > No, it's not the same. 'Convert.ToInt3 2' will return 0 if 'Nothing'
                  > (a null reference) is passed to it, 'Int32.Parse' will throw an
                  > exception.[/color]

                  Otherwise its identical. This oddity doesnt seem to be documented either and seems of limited use.

                  public static int ToInt32(string value);

                  Declaring Type: System.Convert
                  Assembly: mscorlib, Version=1.0.500 0.0

                  public static int ToInt32(string value)
                  {
                  if (value == null)
                  {
                  return 0;
                  }
                  return int.Parse(value );
                  }




                  --
                  Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
                  "Programmin g is an art form that fights back"

                  Get your ASP.NET in gear with IntraWeb!

                  Comment

                  • ru

                    #24
                    Re: Local Settings and NumberFormat issues

                    On Sun, 19 Jun 2005 13:21:37 +0200, "Cor Ligthert"
                    <notmyfirstname @planet.nl> wrote:
                    [color=blue]
                    >Ru,
                    >
                    >I find the CInt an extremly strong instrucion.
                    >
                    >Can you try this.
                    >Set your decimal point to comma.
                    >Have a look if it is done for everything
                    >Create a new form
                    >Drag a textbox on the form
                    >Click on the form and set in than in the loadevent
                    >
                    >dim whatever as integer = Cint("1,00")
                    >
                    >For me the most compatible in system is this one
                    >System.Convert .ToInt32("1,00" )
                    >
                    >However Cint should do it and is less typing.
                    >
                    >I am interested in the result.
                    >
                    >Cor[/color]

                    Hi Cor,

                    sorry for the delay, but I've been working through the weekend.
                    I did as you said, build the form in a new project, set the decimal
                    point to "," then ran the exe, and got the following error on the
                    Cint("1,00") code:

                    Additional information: The currency separator information specified
                    in the NumberFormatInf o is ambiguous for parsing.

                    If I use System.Convert. ToInt32("1,00") or Integer.Parse I get the
                    following error:
                    Additional information: Input string was not in a correct format.

                    Do you get the same results?

                    ru









                    Comment

                    • Cor Ligthert

                      #25
                      Re: Local Settings and NumberFormat issues

                      Ru,

                      I don't have English settings, however I saw something strange when I
                      changed the decimal point in the showed config.

                      My question was what it does with a complete new application with only one
                      textbox.
                      A setting to of the config to comma and one CInt instruction. If that does
                      not work than you can call it in my opinon a bug.

                      I have the idea that it is not directly in Net because from what I saw in
                      that config. (After setting the decimal all showed values where changed
                      except one).

                      For me it is not impossible because of what I saw and what you told that it
                      is a bug in the OS or maybe even more possible one of the security or SP
                      updates, because you told that you did not have it before.

                      However, just a guess

                      Cor


                      Comment

                      Working...