Reputation: 365
I have a legacy VB6 application that writes to a file in random access mode. This file is then read by an application developed by third party.
I have been tasked with rewriting the VB6 app in VB.NET however the third party application will not change. I have attempted to convert the VB6 code to VB.NET however the random access file is not being read correctly.
Provided below are condensed snippets of both the VB6 and VB.NET code. The VB.NET code is successfully writing to the file however, the field lengths are not correct and the application reading the file is not parsing the data correctly. How can I go about writing to the random access file in the same manner?
I have searched around but have not found a solution that works.
VB6
Type Person
ID as String * 5
Name as String * 25
EyeColor as String * 10
End Type
Dim myPerson as Person
myPerson.ID = "13"
myPerson.Name = "Joe"
myPerson.EyeColor = "Blue"
Open <file path> For Random As <file number> Len = Len(myPerson)
Put <file number>, myPerson.ID, myPerson
Close <file number>
VB.NET
Structure Person
<VBFixedString(5)> Dim ID As String
<VBFixedString(25)> Dim Name As String
<VBFixedString(10)> Dim EyeColor As String
End Structure
Dim myPerson as New Person
myPerson.ID = "13"
myPerson.Name = "Joe"
myPerson.EyeColor = "Blue"
FileOpen(<file number>, <file path>, OpenMode.Random, , , Len(myPerson))
FilePut(<file number>, myPerson, myPerson.ID)
FileClose(<file number>)
Upvotes: 2
Views: 2202
Reputation: 2297
Not sure if this solves the encoding problems....
I hardcoded the FilePut RecordNumber in the test.
Structure Person
<VBFixedString(5)> Dim ID As String
<VBFixedString(25)> Dim Name As String
<VBFixedString(10)> Dim EyeColor As String
End Structure
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim myPerson As New Person
myPerson.ID = "13".PadLeft(5)
myPerson.Name = "Joe".PadLeft(15)
myPerson.EyeColor = "Blue".PadLeft(10)
Stop
FileOpen(1, "C:\temp\random.bin", OpenMode.Random, , , Len(myPerson))
FilePut(1, myPerson, 2) ' needs integer RecordNumber - depreciated?
FileClose(1)
End Sub
Visual Studio has a hex editor - I think is an option under File, Open
Upvotes: 0