Search code examples
windowsvb.netwinformsglobalizationmscorlib

Dim b As Integer = Integer.Parse("1") resulting in "System.FormatException: 'Input string was not in a correct format.'"


I'm sure I must be doing something stupid but any ideas why is this not working?

        Dim b As Integer = -1
        b = Integer.Parse("‭1")

If anyone wonders why I'm trying to do this... it is part of debug, originally I wanted to get the number from a text box on a form, however that was not converting with tryParse and throwing the exception with Parse/Convert therefore I trimmed the code down to debug it....

The exception I'm getting is: System.FormatException: 'Input string was not in a correct format.'

More Details:

System.Number.StringToNumber(string, System.Globalization.NumberStyles, ref System.Number.NumberBuffer, System.Globalization.NumberFormatInfo, bool)
System.Number.ParseInt32(string, System.Globalization.NumberStyles, System.Globalization.NumberFormatInfo)
USBreceiver.Form1.Btn_confirm_details_Click(Object, System.EventArgs) in Form1.vb
System.Windows.Forms.Control.OnClick(System.EventArgs)
System.Windows.Forms.Button.OnClick(System.EventArgs)
System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs)
System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message, System.Windows.Forms.MouseButtons, int)
System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message)
System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message)
System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message)
... [Call Stack Truncated]

I've tried various alternate convert functions (tryparse, convert.toint32, etc) but I'm getting the same result... eg. result =0 and exception caught if a tryparsse or just throw the exception. Either way the conversion was failing.

I can't help thinking there's something weird going on with locale / globalisation (I'm in the Uk using VS2019 & .net 4.7.2, it's a VB.net Windows Forms application. The 'source' of the exception is mscorlib StringToNumber..

      Source  "mscorlib"  String
      StackTrace  "   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)" & vbCrLf & "   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)" & vbCrLf & "   at USBreceiver.Form1.Btn_confirm_details_Click(Object sender, EventArgs e) in C:\Users\Z830\source\repos\USBreceiver\USBreceiver\Form1.vb:line 505" & vbCrLf & "   at System.Windows.Forms.Control.OnClick(EventArgs e)" & vbCrLf & "   at System.Windows.Forms.Button.OnClick(EventArgs e)" & vbCrLf & "   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)" & vbCrLf & "   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)" & vbCrLf & "   at System.Windows.Forms.Control.WndProc(Message& m)" & vbCrLf & "   at System.Windows.Forms.ButtonBase.WndProc(Message& m)" & vbCrLf & "   at System.Windows.Forms.Button.WndProc(Message& m)" & vbCrLf & "   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)" & vbCrLf & "   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)" & vbCrLf & "   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)" & vbCrLf & "   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)" & vbCrLf & "   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)" & vbCrLf & "   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()" & vbCrLf & "   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()" & vbCrLf & "   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)" & vbCrLf & "   at USBreceiver.My.MyApplication.Main(String[] Args) in :line 81"   String
      TargetSite  {Void StringToNumber(System.String, System.Globalization.NumberStyles, NumberBuffer ByRef, System.Globalization.NumberFormatInfo, Boolean)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

The strangest thing is I'm sure it has worked before then I've changed the number (to a big number but still smaller than max int32 = 2*10^9) and it broke for good, now won't even work with "1"... it's really weird....

Another jewel of information is I think loading VS2019 (or possibly some other software) was changing my keyboard layout to US... so I have uninstalled US keyboard layout from windows - but it hasn't fixed anything.

Any thoughts? Thanks,


Solution

  • @JohnL you are my saviour!!!!

    I've lost an entire day of time on this and am truely greatful...

    enter image description here

    I'm still not entirely sure where the option lives but typed 'unicode' in to the search bar at the top of VS2019 and there was the tickbox -> Now it works! ''' Save Documents as Unicode when data cannot be saved as codepage '''

    Thanks!!!