0

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,

jteez
  • 13
  • 5
  • There's a problem with encoding of your code file. Save your code file as Unicode. – JohnyL May 23 '20 at 08:47
  • Is the code file the source? or compiled...? where are the options for that, would it be project properties? – jteez May 23 '20 at 08:52
  • Save file with this code as UTF-8 encoding. – JohnyL May 23 '20 at 08:57
  • That `"‭1"` is not `"1"` : you have an unprintable char inside the quotes. If you `Console.WriteLine("‭1")`, you'll see `?1` instead of `1`. It's copy/paste issue. – Jimi May 23 '20 at 09:05
  • Thanks Jimi, also really useful, the "1" was typed out so that was definitly an encoding issue but I did what you said for the text in the textbox and it had the '?' due to a copy and paste... Thank you – jteez May 23 '20 at 10:37

1 Answers1

0

@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!!!

jteez
  • 13
  • 5
  • Thanks JohnyL -> i see the option in the SourceFile 'Save As' - dropdown arrow for Save with Encoding! Thanks! – jteez May 23 '20 at 09:07
  • If you have an answer to your question, even if you posted it yourself, please accept that answer so that we can see that don't need any more help without opening the question and reading the whole thing. – jmcilhinney May 23 '20 at 10:23
  • Also make sure you use Jimi's method "Console.WriteLine()" to test any strings that are missbehaving! – jteez May 23 '20 at 10:37
  • Apparently I've got to wait 2 days to close the answer but I will do, thanks jmcilhinney – jteez May 23 '20 at 10:40
  • I wasn't aware of that. Good to know. – jmcilhinney May 23 '20 at 10:48