[RESOLVED] [02/03] For Loop madness (array problem cont)
Thanks to jm who helped me the resizing array but for some reason I cannot get all the data listed in a listbox.
Now resolved~! feel so silly :eek:
I have a data file "Phonelist" with 3 lists to which can utilised as the actual directories after user selects (& if not exist needs to be created). My problem is that the For Loop only lists the last item (of 3) in the listbox!!! :confused:
BTW you will see that I have been playing around trying to get it to work!
VB Code:
Sub DisplayDir(ByVal fn As String, ByVal path As String)
Dim sr As StreamReader
Dim sw As StreamWriter
Dim temp() As String
Dim prompt, read As String
Dim counter, i, j As Integer
fn = "PHONELISTS.TXT" 'File with phone directories
sr = File.OpenText(f & fn)
counter = 0 'Counts number of directories listed
Do While (sr.Peek <> -1)
counter += 1
sr.ReadLine()
Loop
ReDim temp(counter)
sr.Close()
'i = counter
'Reads file in temp array
sr = File.OpenText(f & fn)
:wave: Do While (sr.Peek <> -1) '[B]I forgot to do a for/next loop[/B]
temp(counter) = sr.ReadLine
Loop
'ReDim temp(i)
sr.Close()
[B] For j = 1 To counter
:confused: lstOutput.Items.Add(temp(counter))
Next[/B]
End Sub
Re: [RESOLVED] [02/03] For Loop madness (array problem cont)
lstOutput.Items.Add(temp(counter))
Change this to:
lstOutput.Items.Add(temp(j))
Counter is fixed, j is the variable.
Re: [RESOLVED] [02/03] For Loop madness (array problem cont)
There's a real problem with that code. You're reading the whole file twice. There's not much point reading an entire file to find out how many lines are in it and thowing away the very data you want to store. What if the file was really big? You basically have three options to read a file like that.
1. Read the entire file into a single string and then split that string into lines:
VB Code:
Dim fileContents As String = myStreamReader.ReadToEnd()
Dim lines As String() = System.Text.RegularExpressions.Regex.Split(fileContents, Environment.NewLine)
2. Use a collection instead of an array. You can copy the data from the collection to an array when you're don if you really need an array:
VB Code:
Dim fileContents As New Specialized.StringCollection
Do Until myStreamReader.Peek() = -1
fileContents.Add(myStreamReader.ReadLine())
Next
Dim lines(fileContents.Count - 1) As String
fileContents.CopyTo(lines, 0)
3. Use an array and resize it as required. The resizing should be done in blocks rather than one element at a time. This is the exact method that simple collections use:
VB Code:
Dim lines(16 - 1) As String 'Start with a capacity of 16.
Dim lineCount As Integer = 0
Do Until myStreamReader.Peek() = -1
If lineCount = lines.Length Then
'The array is full so double its capacity.
ReDim Preserve lines(2 * lineCount - 1)
End If
'Read the next line.
lines(lineCount) = myStreamReader.ReadLine()
lineCount += 1
Loop
'Remove unused elements if desired.
ReDim Preserve lines(lineCount - 1)
Option 1 is preferable as the code is simple but gets less desirable as the file size gets large as reading the file as a block uses more memory. Option 2 is the best general purpose choice. Option 3 is the most efficient but also requires the most complex code.