Imports System.IO
Public Class MainClass
Public Shared Sub Main()
Dim Bob As New Person("Bob", 34, 5.25)
Bob.SaveToFile("c:\a.bin")
Bob = Nothing
Bob = Person.LoadFromFile("c:\a.bin")
System.Console.WriteLine(Bob.Name)
End Sub
End Class
Public Class Person
Public Name As String
Public Age As Integer
Public Height As Integer
Public Sub New()
End Sub
Public Sub New(ByVal Name As String, ByVal Age As String, ByVal Height As String)
Me.Name = Name
Me.Age = Age
Me.Height = Height
End Sub
Public Sub SaveToFile(ByVal Filename As String)
Dim fs As New FileStream(Filename, FileMode.Create)
Dim w As New BinaryWriter(fs)
w.Write(Name)
w.Write(Age)
w.Write(Height)
w.Close()
End Sub
Public Shared Function LoadFromFile(ByVal Filename As String) As Person
Dim fs As New FileStream(Filename, FileMode.Open)
Dim r As New BinaryReader(fs)
Dim NewPerson As New Person()
NewPerson.Name = r.ReadString()
NewPerson.Age = r.ReadInt32()
NewPerson.Height = r.ReadInt32()
r.Close()
Return NewPerson
End Function
End Class
|