Question Off by one?

franchise63

New member
Joined
Aug 9, 2011
Messages
3
Programming Experience
5-10
Hi there,

I have both VB.Net 2003 and 2005 installed in my computer. Recently I developed an small (less than 200kb) application using VB2k5 that converts a text file from an old format to a new one. The "hard" part of the task is to count and parse the characters of the old format to fit the new format, on the assumption that the text file is always in the correct (old) format. The application works fine in my machine, but when I port it as an executable in other machines, it throws a invalidtypecast error, as the characters were not parsed well.

I used the same application and text file in my machine and it works PERFECTLY.

Any suggestions?

Thanks!

Francis
 
Re: off by one?

Hi,

My code parses the contents of a text file using a variety of Mid, Left, and Right functions, with the number arguments entered specifying the exact number of characters alloted for each field. Some areas would be treated as strings while some would be treated as numbers.

One line of the text file is displayed like this:

VB.NET:
20xxxxxxx        xxxxxxxxx      X3312799906    0.00    0.00 1040.00  0.00  0.00  0.00  0.00  0.00 10.00      N18910500

A sample snippet is provided below (it would be too complicated to explain the snippet, but basically it just parses the field and converts it into a new format)

strNewContents = strNewContents & Strings.Left(strContents, 43) & _
Strings.RSet("1560.00", 8) & _
Strings.Mid(strContents, 52, 34) & _
Strings.RSet(Format(CDbl(Strings.Mid(strContents, 86, 6)) + CDbl(Strings.Mid(strContents, 44, 8)) - 1560, "#.00"), 6) & _
Strings.Mid(strContents, 92, 19) & vbCrLf

The entire application works well in my machine, but when the executable runs in other machines it displays the error message:

Value "0 0.00" to type double is not valid (or something like that)

This means that VB parses my string correctly in my machine, but not in others.

Any suggestions as to why this happens?

Thanks!
 
Value "0 0.00" to type double is not valid
Not all cultures use that decimal separator. "0 0.00" is weird selection though, that field value should most likely be "0.00".
I recommend you use the TextFieldParser class to parse the line as fixed-width fields and trim them as necessary, see How to: Read From Fixed-width Text Files in Visual Basic

When converting the field strings into numeric values you have to take into account culture specific formatting if the source comes from a different culture than the system configuration that is executing the application, the Parse methods allow you to specify culture formatting to be used when parsing the string value.
 
Hi JohnH,

No culture specific concerns there.

If you could see the sample line of my text file, my code should have gotten " 0.00" but instead it got "0 0.0" (the last "0" should've been omitted in my previous post). The weird thing is that it works in my machine (meaning my code parses the text to get " 0.00" and could compute it as a decimal) but not in others since the count is off by one.

Anyway I\ll try to read through the link you provided. Hopefully I'll learn something more from that, as well.

Thanks! Cheers!
 

Latest posts

Back
Top