Simple Serializable Person Object : Serialize Object « File Directory « VB.Net






Simple Serializable Person Object

   
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

   
    
    
  








Related examples in the same category

1.Serialize Data to Binary and XML at the same time
2.Serialize Object to XML Stream and output to screen
3.Save Serializable Object to binary file: create a sequential-access file
4.Serializable Person Object
5.Serialize to Xml
6.MarshalByRefObject access objects across application domain