Imports System.IO
public class Test
public Shared Sub Main
Dim emp As New Employee
Dim file_num As Integer = FreeFile()
FileOpen(file_num, "MYFILE.DAT", OpenMode.Random, _
OpenAccess.ReadWrite, OpenShare.Shared, _
Len(emp))
FilePut(file_num, New Employee(1, "A", "A"))
FilePut(file_num, New Employee(2, "B", "B"))
FilePut(file_num, New Employee(3, "C", "C"))
Dim obj As ValueType = DirectCast(emp, ValueType)
For Each i As Integer In New Integer() {3, 1, 2}
FileGet(file_num, obj, i)
emp = DirectCast(obj, Employee)
Console.WriteLine(emp.ToString())
Next i
FileClose(file_num)
End Sub
End class
Public Structure Employee
Public ID As Integer
<VBFixedString(15)> Public FirstName As String
<VBFixedString(15)> Public LastName As String
Public Sub New(ByVal new_id As Integer, ByVal first_name As String, _
ByVal last_name As String)
ID = new_id
FirstName = first_name
LastName = last_name
End Sub
Public Overrides Function ToString() As String
Return ID & ": " & FirstName & " " & LastName
End Function
End Structure
Imports System.IO
Structure Product
Dim ProdID As String
Dim ProdDescription As String
Dim ListPrice As Single
Dim Available As Boolean
Dim MinStock As Integer
End Structure
public class Test
public Shared Sub Main
Dim objBR As BinaryReader
Dim objFS As FileStream
Dim objProduct As New Product
objFS = New FileStream("Records.bin", FileMode.Open, FileAccess.Read)
objBR = New BinaryReader(objFS)
objBR.BaseStream.Seek(0, SeekOrigin.Begin)
While objFS.Position < objFS.Length
objProduct = Nothing
With objProduct
.ProdID = objBR.ReadString
.ProdDescription = objBR.ReadString
.ListPrice = objBR.ReadSingle
.Available = objBR.ReadBoolean
.MinStock = objBR.ReadInt32
End With
ShowRecord(objProduct)
End While
objBR.Close()
objFS.Close()
End Sub
Private Shared Sub ShowRecord(ByVal objRecord As Product)
Console.WriteLine(objRecord.ProdDescription)
Console.WriteLine(objRecord.ListPrice.ToString)
Console.WriteLine(objRecord.Available)
Console.WriteLine(objRecord.MinStock.ToString)
End Sub
End class
Imports System.IO
Structure Product
Dim ProdID As String
Dim ProdDescription As String
Dim ListPrice As Single
Dim Available As Boolean
Dim MinStock As Integer
End Structure
public class Test
public Shared Sub Main
Dim objBW As BinaryWriter
Dim objFS As FileStream
Dim objProduct As Product
objFS = New FileStream("Records.bin", FileMode.OpenOrCreate, FileAccess.Write)
objBW = New BinaryWriter(objFS)
objBW.BaseStream.Seek(0, SeekOrigin.Begin)
objProduct = New Product
With objProduct
.ProdID = "1"
.ProdDescription = "AAA"
.ListPrice = 4.99
.Available = True
.MinStock = 40
End With
SaveRecord(objBW, objProduct)
objProduct = New Product
With objProduct
.ProdID = "2"
.ProdDescription = "BBB"
.ListPrice = 0.99
.Available = True
.MinStock = 1000
End With
SaveRecord(objBW, objProduct)
objBW.Close()
objFS.Close()
End Sub
Private Shared Sub SaveRecord(ByVal objWriter As BinaryWriter, ByVal objRecord As Product)
With objWriter
.Write(objRecord.ProdID)
.Write(objRecord.ProdDescription)
.Write(objRecord.ListPrice)
.Write(objRecord.Available)
.Write(objRecord.MinStock)
End With
End Sub
End class