Join 136,560 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,923 people online right now. Registration is fast and FREE... Join Now!
The object of this code is to extract data from a txt file and make it usable in a form. the program will actually do this HOWEVER, I get an exception error (on the line I have changed to red (I think)) the actual line that get the exception error is," _decPrice(intCount) = Convert.ToDecimal(objReader.ReadLine()) ". How do I oover come this exception error? or Can I just remove the line and still get the same results that I want?
Script in vb 2005
CODE
Dim _strLocationandNameofFile As String = "F:\MiramarBooks.txt" Dim strNameItem As String 'The ListBox object is filled with the Names of the Books held in the Inventory. Dim objReader As IO.StreamReader Dim intCount As Integer Dim strFileErrorMessage As String = "The File is not available. Restart when it is available."
If IO.File.Exists(_strLocationandNameofFile) Then objReader = IO.File.OpenText(_strLocationandNameofFile)
'Read the file line by line until the file is completed While Not objReader.EndOfStream
If intCount = 9 Then ReDim Preserve _strInventoryItem(_intSizeOfArray + 10) ReDim Preserve _strName(_intSizeOfArray + 10) ReDim Preserve _decPrice(_intSizeOfArray + 10) ReDim Preserve _intQuantityonHand(_intSizeOfArray + 10) ReDim Preserve _intQuantityonOrder(_intSizeOfArray + 10) End If
If Not objReader.EndOfStream Then
_strInventoryItem(intCount) = objReader.ReadLine() [color=#FF0000]_decPrice(intCount) = Convert.ToDecimal(objReader.ReadLine())[/color] _intQuantityonHand(intCount) = Convert.ToInt32(objReader.ReadLine()) _intQuantityonOrder(intCount) = Convert.ToInt32(objReader.ReadLine()) intCount += 1 End If End While objReader.Close() End If
Dim topIndex As Integer Dim n As Integer Dim Order As Integer Dim OnHand As Integer Dim dTotalOH As Decimal Dim dTotalOnO As Decimal topIndex = intCount - 1
For n = 0 To topIndex Me.lstBooks.Items.Add(_strInventoryItem(n))
Try taking a look at the TryParse method of Decimal. This method takes a string and parses it to a Decimal for you, which you provide as a parameter. The return value is true/false to let you know if the parse worked.
Code compliments of MSDN...
vb
Dim value As String Dim number As Decimal
' Parse a floating-point value with a thousands separator. value = "1,643.57" If Decimal.TryParse(value, number) Then Console.WriteLine(number) Else Console.WriteLine("Unable to parse '{0}'.", value) End If
Notice that they give it the variable "number" which if the parse is successful it stashes the value in that variable. It is then used in an if statement to test if the parse was successful.
More information can be found at the link below...
Thank you for your reply but I am obviously not bright enough to figure this one out. The decimals I am working with are not a set number but vary such as 24.95, 18.95, 35.65. My other question is would it require me to change the rest of my code if I do this?
You mean that is all I needed to change was one line? I have been pulling my hair out trying to figure how to rewrite everything to include the Tryparse. I can see I have alot to learn before I get beyind the grasshopper stage.
Thank you again.
Oh why was my original post moved from its location do you know?