Conversion from string "0" to type 'Integer' is not valid

G

Guest

Hi all,

I'm looking for a little help in determining what's causing this exception.
I'm getting it when running VB.NET apps developed in VS 2005 (.NET 2.0). It
doesn't appear to matter under what context the conversion takes place -
whether it's implicit or explicitly cast - it barfs either way. The source
of the string "0" doesn't appear to matter either...whether it's from a text
box, data grid, or simply coded directly, it throws the exception every time.
The following will fail, for example (option strict: off)...

Dim s As String = "0"
Dim i As Integer

i = s

The problem is specific to my machine and 1 other colleague's...it works
fine for everybody else. Furthermore, running a VB.NET app developed in VS
2003 (.NET 1.1) from my machine does NOT produce the error. I tried
re-installing the 2.0 framework, but still get the exception. Full text of
the exception from the above code follows...

System.InvalidCastException: Conversion from string "0" to type 'Integer' is
not valid. ---> System.FormatException: Input string was not in a correct
format.
at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String
Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String
Value)
--- End of inner exception stack trace ---
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String
Value)
at TestInt0.Form1.Button1_Click(Object sender, EventArgs e) in
C:\VS_Temp\TestInt0\TestInt0\Form1.vb:line 36

Any help would be much appreciated! Thanks so much!

Steve
 
P

Patrick Steele

Hi all,

I'm looking for a little help in determining what's causing this exception.
I'm getting it when running VB.NET apps developed in VS 2005 (.NET 2.0). It
doesn't appear to matter under what context the conversion takes place -
whether it's implicit or explicitly cast - it barfs either way. The source
of the string "0" doesn't appear to matter either...whether it's from a text
box, data grid, or simply coded directly, it throws the exception every time.
The following will fail, for example (option strict: off)...

Dim s As String = "0"
Dim i As Integer

i = s

The problem is specific to my machine and 1 other colleague's...it works
fine for everybody else. Furthermore, running a VB.NET app developed in VS
2003 (.NET 1.1) from my machine does NOT produce the error. I tried
re-installing the 2.0 framework, but still get the exception. Full text of
the exception from the above code follows...

System.InvalidCastException: Conversion from string "0" to type 'Integer' is
not valid. ---> System.FormatException: Input string was not in a correct
format.
at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String
Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String
Value)
--- End of inner exception stack trace ---
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String
Value)
at TestInt0.Form1.Button1_Click(Object sender, EventArgs e) in
C:\VS_Temp\TestInt0\TestInt0\Form1.vb:line 36

It worked for me too (with Option Strict Off). Could be a regional
settings thing. Are all of your regional settings the same (Number
format, decimal character, thousands separator, etc...)
 
R

RobinS

This is the worst programming method ever. Why in the world would you even
do that? Do you have Option Strict On in your VB projects? This wouldn't
compile if you did. If you have some need to convert a string to an
integer, do it specifically. Code like this causes weird problems that is
really difficult to track down.

Dim s as String = "0"
Dim i as Integer
i = CType(s, Integer)

Robin S.
 
G

Guest

Thanks for your response Patrick! My regional settings are identical to
those of another colleague's who doesn't receive the exception, and there's
no setting that indicates a 0 should not be treated as an integer. I would
also think that the absence of the exception in VS 2003 would imply that the
cause isn't an OS regional setting.
 
G

Guest

I appreciate your response RobinS, but apparently you didn't read my post
very thoroughly. I specified that I have Option Strict Off and that this
exception is thrown regardless of the context of the conversion, whether it
be implicit or explicitly cast. Explicitly converting the string as you have
indicated below will still throw the exception on my machine. The problem
was identified when I was trying to convert the text in a text box into an
integer. The simple code I provided is merely an example of something that
should work (with option strict off) that doesn't.
 
G

Guest

I discovered the solution to this should anyone else run into it...

Modify the HKEY_CURRENT_USER\Control Panel\International\sPositiveSign
registry key so that its value is nothing. If it appears as though the value
is already empty (as it did in my case), edit the data string to some
arbitrary viewable character, save it, open it up again and remove the value.
 
R

RobinS

Yes, I did get that. My point was, "Why do you care, since this is not good
programming practice and you shouldn't be doing it this way anyway?" ;-)

Robin S.
--------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top