Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 26th, 2012, 05:43 PM
Registered User
 
Join Date: Nov 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old December 31st, 2012, 07:54 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi Brandon,

you probably want to use arrFileLP(intIndex) or you will definitely get an IndexOutOfRangeException.

Also I guess the 2nd FileReader should read _InventoryFile not FileName_IN?

HTH
Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
CHAPTER 3 - Outlets and Actions TRY IT OUT program is not working tduraipkt BOOK: Beginning iPad Application Development 2 August 25th, 2010 05:47 AM
How to tell the user that the program is working? chobo2 C# 1 November 10th, 2007 06:05 AM
Working with Excel from VB6 program aarmit10 Pro VB 6 0 March 17th, 2006 06:36 AM
Setup Project: Program not added in Start>Program arif_1947 VS.NET 2002/2003 2 March 31st, 2005 06:40 AM
Linked Table working very slow with client program Nerijus Access 0 July 7th, 2003 08:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.