Results 1 to 6 of 6

Thread: [RESOLVED] Help with this VB 2005 program

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    7

    Resolved [RESOLVED] Help with this VB 2005 program

    This VB program is supposed to merge two files together, part1.txt and part2.txt, and store it in everyone.txt. Files each have a first name, last name, and an id on each line. Parts 1 and 2 are both already sorted in alphabetical order by last name, and the program needs to move the contents of 1 and 2 into everyone with it still being sorted by last name. The problem is, this program fully writes part1 to everyone.txt before it even starts writing part2. I can't seem to figure out why. It's like it only ever thinks that theData1(1) > theData2(1). Please help. Thanks.

    Code:
    Public Class Form1
    
        Structure People 'Defines the People structure
    
            Dim FirstName, LastName As String
            Dim Id As Integer
        End Structure
    
        Dim Everyone(1) As People
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim mySr1 As New IO.StreamReader("C:\part1.txt")
            Dim mySr2 As New IO.StreamReader("C:\part2.txt")
            Dim mySw As New IO.StreamWriter("C:\Everyone.txt")
    
            Dim aStrLine1, aStrLine2 As String 'To hold an inputted string
            Dim theData1, theData2 As Object 'To temporarily hold inputted values
    
            ReadFromPart1(mySr1, aStrLine1, theData1)
            ReadFromPart2(mySr2, aStrLine2, theData2)
    
            While (Not mySr1.EndOfStream) And (Not mySr2.EndOfStream)
    
                If ((theData1(1)) > (theData2(1))) Then
                    WriteFromPart1(mySw)
                    ReadFromPart1(mySr1, aStrLine1, theData1)
                End If
    
                If ((theData1(1)) < (theData2(1))) Then
                    WriteFromPart2(mySw)
                    ReadFromPart2(mySr2, aStrLine2, theData2)
                End If
    
                If ((theData1(1)) = (theData2(1))) Then
                    WriteFromPart1(mySw)
                    ReadFromPart1(mySr1, aStrLine1, theData1)
    
                    WriteFromPart2(mySw)
                    ReadFromPart2(mySr2, aStrLine2, theData2)
    
                End If
    
                If (mySr1.EndOfStream) Then
                    While (Not mySr2.EndOfStream)
                        WriteFromPart2(mySw)
                        ReadFromPart2(mySr2, aStrLine2, theData2)
                    End While
                End If
    
                If (mySr2.EndOfStream) Then
                    While (Not mySr1.EndOfStream)
                        WriteFromPart1(mySw)
                        ReadFromPart1(mySr1, aStrLine1, theData1)
                    End While
    
                End If
    
            End While
    
            mySr1.Close()
            mySr2.Close()
            mySw.Close()
    
        End Sub
    
        Sub WriteFromPart1(ByRef mySw As IO.StreamWriter)
            mySw.WriteLine(Everyone(0).FirstName & " " & Everyone(0).LastName & " " & Everyone(0).Id)
        End Sub
    
        Sub WriteFromPart2(ByRef mySw As IO.StreamWriter)
            mySw.WriteLine(Everyone(1).FirstName & " " & Everyone(1).LastName & " " & Everyone(1).Id)
        End Sub
    
        Sub ReadFromPart1(ByRef mySr1 As IO.StreamReader, ByRef aStrLine1 As String, ByRef theData1 As Object)
            aStrLine1 = mySr1.ReadLine
            theData1 = Split(aStrLine1, " ")
            Everyone(0).FirstName = theData1(0)
            Everyone(0).LastName = theData1(1)
            Everyone(0).Id = theData1(2)
        End Sub
    
        Sub ReadFromPart2(ByRef mySr2 As IO.StreamReader, ByRef aStrLine2 As String, ByRef theData2 As Object)
            aStrLine2 = mySr2.ReadLine
            theData2 = Split(aStrLine2, " ")
            Everyone(1).FirstName = theData2(0)
            Everyone(1).LastName = theData2(1)
            Everyone(1).Id = theData2(2)
        End Sub
    
    End Class
    Last edited by Motoidar; Nov 11th, 2007 at 10:30 PM.

  2. #2

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    7

    Re: Help with this VB 2005 program

    Nobody can figure this out?

  3. #3
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: Help with this VB 2005 program

    I'd read in everybody from both files into a single List(Of People), then loop through that list AFTER you've fully closed both files and write it all to the new file.

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

    Re: Help with this VB 2005 program

    What TS suggested is definitely the way to go. It will be much cleaner and easier.

    However, if you want to get what you have working, you are very close. This is a case where you need to set a breakpoint early in the code and step through it. Make sure that the two items being read are what you expect them to be, then step through the loop to see which branch you are taking. I can think of a couple issues that might be arising from your code, but without stepping through it, I would just as likely focus on the wrong piece.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    7

    Re: Help with this VB 2005 program

    After further testing, I have found out that it only wants to enter the first if statement where theData1(1) > theData2(1). That is many times not true, but it still does it.

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    7

    Re: [RESOLVED] Help with this VB 2005 program

    I figured it out. The lessthan and greaterthan signs needed to be switched.

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