Results 1 to 5 of 5

Thread: [RESOLVED] Loop in Loop error

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    39

    Resolved [RESOLVED] Loop in Loop error

    Hey guys, in my project I am trying to have the program write to a richtextbox which gathers data from files using a loop inside of a loop. The program works OK, it finds the files and reads them but the richtextbox should be returning two values but instead its only returning one. Can anyone help me with this is there any errors in my code. Here's the code:


    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim totalFiles As String
            Dim filePaths As New System.Text.StringBuilder
            Dim fileCount As Integer = 0
    
            For Each file As String In My.Computer.FileSystem.GetFiles(Application.StartupPath & "\files")
                fileCount += 1
                filePaths.Append(My.Computer.FileSystem.GetName(file) & vbCrLf)
                totalFiles = filePaths.ToString()
    
            Next
    
            RichTextBox1.Text = filePaths.ToString()
            TextBox1.Text = fileCount
            System.IO.File.WriteAllText(Application.StartupPath & "\data\values.txt", filePaths.ToString())
    
            Dim getFiles As IO.StreamReader = New IO.StreamReader(Application.StartupPath & "\data\values.txt")
            Dim getLine As String = Nothing
            Dim getLineCount As Integer = 0
    
    
            Do While Not getFiles.EndOfStream
                Dim infoFile As String = getFiles.ReadLine()
    
                getFiles.ReadLine()
    
                getLine = getFiles.ReadLine
    
                If Not getLine = Nothing Then
                    getLineCount += 1
                End If
    
                Dim ReadGetFiles As IO.StreamReader = New IO.StreamReader(Application.StartupPath & "\files\" & infoFile)
                Dim readgetLine As String = Nothing
                Dim readgetLineCount As Integer = 0
    
                Dim lineone As String = Nothing
                Dim linetwo As String = Nothing
                Dim linethree As String = Nothing
                Dim linefour As String = Nothing
                Dim linefive As String = Nothing
                Dim linesix As String = Nothing
    
                Do While Not ReadGetFiles.EndOfStream
                    readgetLine = ReadGetFiles.ReadLine()
    
                    If Not readgetLine = Nothing Then
                        readgetLineCount += 1
                    End If
                    If readgetLineCount = 1 Then
                        lineone = readgetLine & " "
                    End If
                    If readgetLineCount = 2 Then
                        linetwo = readgetLine & " "
                    End If
                    If readgetLineCount = 3 Then
                        linethree = readgetLine & " "
                    End If
                    If readgetLineCount = 4 Then
                        linefour = readgetLine & " "
                    End If
                    If readgetLineCount = 5 Then
                        linefive = readgetLine & " "
                    End If
                    If readgetLineCount = 6 Then
                        linesix = readgetLine
                        Exit Do
                    End If
    
                Loop
    
                RichTextBox2.Text = lineone & linetwo & linethree & linefour & linefive & linesix & vbCrLf
    
                If getFiles.EndOfStream Then
                    Exit Do
                End If
            Loop
        End Sub

    Any help would be much appreciated and thanks for your patience.

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

    Re: Loop in Loop error

    What do you mean when you say that the richtextbox should be returning two values? It isn't returning anything.

    Have you stepped through the code? You could put a breakpoint near the beginning of the function, then use F11 to step line by line through the code looking at the values in the variables. This will probably show where the error is.

    Also, you could speed this up and shrink it down if you used an array of string instead of all the linex variables. Then the readgetLineCount variable would be an index into the array, so all those conditionals could be dispensed with.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    39

    Re: Loop in Loop error

    Quote Originally Posted by Shaggy Hiker
    What do you mean when you say that the richtextbox should be returning two values? It isn't returning anything.

    Have you stepped through the code? You could put a breakpoint near the beginning of the function, then use F11 to step line by line through the code looking at the values in the variables. This will probably show where the error is.

    Also, you could speed this up and shrink it down if you used an array of string instead of all the linex variables. Then the readgetLineCount variable would be an index into the array, so all those conditionals could be dispensed with.
    Yes, the richtextbox1 returns the value of the variables Ok, but only does the first file in the values.txt, if you get what I mean.

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

    Re: Loop in Loop error

    Oh, I see it. Each time through the outer loop you call this:

    vb Code:
    1. RichTextBox2.Text = lineone & linetwo & 'etc

    That will work each time, but it will overwrite the line each time through the loop, so that only that which is written by the final iteration of the loop will be displayed. What you need to do is write it like this:

    vb Code:
    1. RichTextBox2.Text [B]&[/B]= lineone & linetwo & linethree & linefour & linefive & linesix & vbCrLf

    That one character will solve the problem.

    I would also note that the condition for the loop is GetFiles.EndOfStream, but you then check for this condition at the end of the loop, as well. There is no need for that check at the end of the loop, as the check will be performed at the beginning of the next iteration. In other words, this chunk:

    vb Code:
    1. If getFiles.EndOfStream Then
    2.                 Exit Do
    3.             End If

    does nothing useful.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    39

    Re: Loop in Loop error

    Thankyou shaggy hiker, you've been most helpful.

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