Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I have a program that I wrote in VB6. I'm attempting to upgrade it to VB2008. The VB6 program uses a binary data file full of records from a user-defined type. I was able to open the thing and read it into the data array in one step in vb6:
VB
Private Type eRecord
     startdate as Date
     fName as string
     lName as string
     dep as string
End Type

Dim datalist() as eRecord

VB
Open Datfile for Binary as #fnum
Get #fnum, , reclen
Redim datalist(reclen)
Get #fnum, , datalist
Close fnum

Done. Easy as pie

Now I've spent the past hour and a half searching through youtube videos and code websites looking for how to do this in VB2008 and there's never an explanation I can use. I've got the structure for "datalist" set up, and that's as far as I got. (There's a function to automatically upgrade vb6 code, but that failed. So I'm starting from scratch.)

Anyway, a simple, easy-to-understand code snippet would be greatly appreciated. Thanks in advance.

VB
Structure eRecord
     startdate as Date
     fName as string
     lName as string
     dep as string
end Stucture

Dim datalist() as eRecord
Posted

1 solution

This is by far not a nice approach to data storage. The problem is not the binary representation of data itself, but it's ad-hoc and non-flexible nature.

Anyway, you need to use the classes System.IO.BinaryReader and System.IO.BinaryWriter:
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx[^].

Basically, you would need to read and write the structure by component:
C#
using System.IO;

//...

BinaryReader reader = new BinaryReader(/* ... */);
//...
eRecord myRecord = //...

// your structure fields are non-public; you need to change them into public or internal,
// or, better, represent them via public or internal properties; thank you can do this:

// read date here, see below
myRecord.fName = reader.ReadString();
myRecord.lName = reader.ReadString();
myRecord.dep = reader.ReadString();


I don't know how you represented the date, so chances are, you would need to bread date into some primitive-type components like some integer types and read the one-by-one, using reader.ReadUint32, reader.ReadUInt16, reader.ReadByte or the like. You can check with some binary editor to see its binary structure or figure out from original code.

Writing is done in the same way with BinaryWriter, symmetrical to reading.

Next time, don't do such things. Learn serialization instead:
http://en.wikipedia.org/wiki/Serialization#.NET_Framework[^],
http://msdn.microsoft.com/en-us/library/ms233843.aspx[^].

First of all, as a very easy-to-use, robust and universal approach to serialization, consider using Data Contracts:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Good luck,
—SA
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900