Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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
Kevnar1.9K

1 solution

Rate this: bad
good
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:
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:
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
  Permalink  
v5

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

  Print Answers RSS
0 Richard MacCutchan 335
1 OriginalGriff 330
2 Sergey Alexandrovich Kryukov 259
3 Sharmanuj 146
4 Magic Wonder 129
0 Sergey Alexandrovich Kryukov 5,969
1 OriginalGriff 5,005
2 CPallini 2,473
3 Richard MacCutchan 1,547
4 Abhinav S 1,507


Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2014
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