Results 1 to 6 of 6

Thread: [RESOLVED] Help with this VB 2005 program

Threaded View

  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.

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