Trying to get my program working.
I am trying to get this application to sort and separate a .txt file by first two characters in a string. I have the code pretty close to working, just getting an index error now that I don't have the experience to figure out where I went wrong any help would be appreciated. here is the code for the Sub that I have. the Code in red is where i am getting my exception error if that helps.
Private Sub LoadInventoryFile(ByVal FileName_IN As String)
'clear the list
ClearList()
'declare an object to read the file
Dim objReader As IO.StreamReader
'Declare string to hold the line from the file
Dim strLine As String = ""
'Declare a counter for the number of lines in a file
Dim intNumberLPLines As Integer = 0
Dim intNumberPCLines As Integer = 0
'Declare an ARRAY of Strings (initialize to 1 item)
Dim arrFileLP(1) As String
Dim arrFilePC(1) As String
'Declare an index counter for the ARRAY
Dim intLPIndex As Integer = 0
Dim intPCIndex As Integer = 0
'check for the existence of the file
If IO.File.Exists(FileName_IN) Then
'read the file
objReader = IO.File.OpenText(FileName_IN)
'read to get the # of lines in the file (to ReSize the ARRAY)
Do While objReader.Peek <> -1
strLine = objReader.ReadLine()
If Mid(strLine, 1, 2) = "LP" Then
intNumberLPLines += 1
ElseIf Mid(strLine, 1, 2) = "PC" Then
intNumberPCLines += 1
End If
Loop
'Close the File
objReader.Close()
'ReSize the ARRAY to hold the lines from the file
ReDim Preserve arrFileLP(intNumberLPLines - 1)
ReDim Preserve arrFilePC(intNumberPCLines - 1)
'Read the File into the ARRAY
If IO.File.Exists(_InventoryFile) Then
objReader = IO.File.OpenText(FileName_IN)
Do While objReader.Peek <> -1
strLine = objReader.ReadLine()
If Mid(strLine, 1, 2) = "LP" Then
'If item starts with LP
arrFileLP(intLPIndex) = strLine
intLPIndex += 1
ElseIf Mid(strLine, 1, 2) = "PC" Then
'if item starts with PC
arrFilePC(intPCIndex) = strLine
intPCIndex += 1
End If
Loop
'close the file
objReader.Close()
Else
MsgBox(_UnavailableMsg)
Exit Sub
End If
'Sort the Array before adding new inventory to the listbox
Array.Sort(arrFileLP)
Array.Sort(arrFilePC)
'Loop through the LP ARRAY, adding to the listbox
For intIndex = 0 To arrFileLP.Length - 2
lstLPInventory.Items.Add(arrFileLP(intLPIndex))
Next
'Loop through the PC ARRAY, adding to the listbox
For intPCIndex = 0 To arrFilePC.Length - 2
lstPCInventory.Items.Add(arrFilePC(intPCIndex))
Next
Else
MsgBox(_UnavailableMsg)
Exit Sub
End If
End Sub
|