Click here to Skip to main content
12,892,796 members (48,659 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
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:
Private Type eRecord
     startdate as Date
     fName as string
     lName as string
     dep as string
End Type
Dim datalist() as eRecord

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.

Structure eRecord
     startdate as Date
     fName as string
     lName as string
     dep as string
end Stucture
Dim datalist() as eRecord
Posted 25-Sep-12 5:46am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:[^],[^].

Basically, you would need to read and write the structure by component:
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:[^],[^].

First of all, as a very easy-to-use, robust and universal approach to serialization, consider using Data Contracts:[^].

Good luck,

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100