Random I/O and VB Structures

Alternative I/O Methods to streamreader/streamwriter --

 

Binary Files  - 

Random Files -


'  What it was in VB6.0

open filename for random as filenumber len = recordlength

For example, open commondialog1.filename for random as #1 len=56


Visual Basic Structures -

Consider the following record layout for the employees file --

structure person

    public    ID as integer

    public    salary as long

    public    reviewDate as long

    <vbfixedstring(15)> public first as string

    <vbfixedstring(20)> public last as string

end structure

'  Other variables ...

public employee as person

public position as long    'record number in file

public lastrecord as long    'record number of last record


For the VB6 Weasel example - type/end type instead of structure/end structure

Weasel

type Weasel

name as string * 32   'fixed length = 32 chars

color as string * 12  'fixed length = 12 chars

weight as integer

length as integer

birthdate as double

end type


For record layouts, you must fix the size of string variables

 


Reading & Writing to Random Access Files

record #

name

color

weight

length

birthdate

1

sigmund

red

18

11

04/05/94

2

sigfried

brown

14

10

03/15/93

3

...

..

...

...

...


Download the Weasel project components and run the program; then examine the code; convert it to VB.NET.  We will review this in class as well.

weasels for both VB6 AND VB.NET

weasel.vbp    weasel.frm        weasel.dat    weasel.bas 
project file    form/code file  data file    module file


Created & maintained by DrB. Last update:  Wednesday, February 18, 2009