Results 1 to 3 of 3

Thread: [RESOLVED] [02/03] For Loop madness (array problem cont)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Location
    NZ
    Posts
    178

    Resolved [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
    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!!!
    BTW you will see that I have been playing around trying to get it to work!

    VB Code:
    1. Sub DisplayDir(ByVal fn As String, ByVal path As String)
    2.     Dim sr As StreamReader
    3.     Dim sw As StreamWriter
    4.     Dim temp() As String
    5.     Dim prompt, read As String
    6.     Dim counter, i, j As Integer
    7.     fn = "PHONELISTS.TXT" 'File with phone directories
    8.     sr = File.OpenText(f & fn)
    9.     counter = 0 'Counts number of directories listed
    10.     Do While (sr.Peek <> -1)
    11.       counter += 1
    12.       sr.ReadLine()
    13.     Loop
    14.     ReDim temp(counter)
    15.     sr.Close()
    16.     'i = counter
    17.  
    18.     'Reads file in temp array
    19.     sr = File.OpenText(f & fn)
    20.     :wave:  Do While (sr.Peek <> -1) '[B]I forgot to do a for/next loop[/B]
    21.       temp(counter) = sr.ReadLine
    22.     Loop
    23.     'ReDim temp(i)
    24.     sr.Close()
    25.    [B] For j = 1 To counter
    26.       :confused:  lstOutput.Items.Add(temp(counter))
    27.     Next[/B]
    28.   End Sub
    Last edited by lionelnz; May 10th, 2006 at 07:02 PM. Reason: Resolved my own problem

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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.
    My usual boring signature: Nothing

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim fileContents As String = myStreamReader.ReadToEnd()
    2. 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:
    1. Dim fileContents As New Specialized.StringCollection
    2.  
    3. Do Until myStreamReader.Peek() = -1
    4.     fileContents.Add(myStreamReader.ReadLine())
    5. Next
    6.  
    7. Dim lines(fileContents.Count - 1) As String
    8.  
    9. 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:
    1. Dim lines(16 - 1) As String 'Start with a capacity of 16.
    2.         Dim lineCount As Integer = 0
    3.  
    4.         Do Until myStreamReader.Peek() = -1
    5.             If lineCount = lines.Length Then
    6.                 'The array is full so double its capacity.
    7.                 ReDim Preserve lines(2 * lineCount - 1)
    8.             End If
    9.  
    10.             'Read the next line.
    11.             lines(lineCount) = myStreamReader.ReadLine()
    12.             lineCount += 1
    13.         Loop
    14.  
    15.         'Remove unused elements if desired.
    16.         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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width