FormatExceptionWasUnhandled bug
-
I need help with a bug in a program for a PPC device. The following line of code results in a “FormatExceptionWasUnhandled” error:
If Mid(DecimalPart, 1, 1) <> "1" Then
DecimalPart is a String variable containing the text “.5”. When I set up a Watch forMid(DecimalPart, 1, 1)
the result is “.” and forMid(DecimalPart, 1, 1) <> "1"
the result is “True”. I am sure I am missing something incredably simple, I just can not figure out what. Thanks,David Wilkes
-
I need help with a bug in a program for a PPC device. The following line of code results in a “FormatExceptionWasUnhandled” error:
If Mid(DecimalPart, 1, 1) <> "1" Then
DecimalPart is a String variable containing the text “.5”. When I set up a Watch forMid(DecimalPart, 1, 1)
the result is “.” and forMid(DecimalPart, 1, 1) <> "1"
the result is “True”. I am sure I am missing something incredably simple, I just can not figure out what. Thanks,David Wilkes
The FormatException gets thrown when the formatting for an argument does match the parameter specifications of the method. There's nothing in this code that would throw that exception because the Mid function doesn't expect the string parameter to be in any format. Besides, why are you using the old VB6 Mid function? You should be using the methods in the String class, like
If DecimalPart.StartsWith("1") Then
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
The FormatException gets thrown when the formatting for an argument does match the parameter specifications of the method. There's nothing in this code that would throw that exception because the Mid function doesn't expect the string parameter to be in any format. Besides, why are you using the old VB6 Mid function? You should be using the methods in the String class, like
If DecimalPart.StartsWith("1") Then
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Thanks for the reply! This is what I get for being lazy. Using MID? I was using a function someone else wrote (no sense reinventing the wheel right?). I figured out last night that it was part of the line just prior to this that was actually throwing the error (not used to this environment yet). When I stripped it down I figured out it was trying to do Val(".") So I just did what I should have done from the start and write my own function.
David Wilkes