Class Serialization : Class Serialization « Class Module « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Class Module » Class Serialization 
6.45.1.Class Serialization
Imports System.Runtime.Serialization

Public Class Tester
    Public Shared Sub Main
        Dim newEmp As New Employee
        Dim outFile As IO.FileStream
        Dim formatter As New Formatters.Binary.BinaryFormatter

        newEmp.FullName = "John Doe"
        newEmp.HireDate = #11/7/2005#
        newEmp.Salary = 10000@

        outFile = New IO.FileStream("EmpData.dat",IO.FileMode.Create)

        formatter = New Formatters.Binary.BinaryFormatter
        formatter.Serialize(outFile, newEmp)

        outFile.Close()



        Dim oldEmp As Employee = Nothing
        Dim inFile As IO.FileStream

        inFile = New IO.FileStream("EmpData.dat",IO.FileMode.Open)

        formatter = New Formatters.Binary.BinaryFormatter
        oldEmp = CType(formatter.Deserialize(inFile), Employee)
        inFile.Close()

        Console.WriteLine(oldEmp.FullName)
        Console.WriteLine(oldEmp.HireDate.ToString())
        Console.WriteLine("Salaray: " & oldEmp.Salary.ToString())
    End Sub
End Class



<Serializable()> _
Public Class Employee
    Implements ISerializable

    Public FullName As String
    Public HireDate As Date
    Private CurrentSalary As Decimal
    Public Property Salary() As Decimal
        Get
            Return CurrentSalary
        End Get
        Set(ByVal value As Decimal)
            CurrentSalary = value
        End Set
    End Property

    Public Sub New()
    End Sub

    Public Sub New(ByVal info As SerializationInfo, _
          ByVal context As StreamingContext)
        FullName = info.GetString("FullName")
        HireDate = info.GetDateTime("HireDate")
        CurrentSalary = info.GetDecimal("Salary")
    End Sub

    Public Sub GetObjectData_
          ByVal info As SerializationInfo, _
          ByVal context As StreamingContext_
          Implements ISerializable.GetObjectData
        info.AddValue("FullName", FullName)
        info.AddValue("HireDate", HireDate)
        info.AddValue("Salary", CurrentSalary)
    End Sub
End Class
John Doe
07/11/2005 12:00:00 AM
Salaray: 10000
6.45.Class Serialization
6.45.1.Class Serialization
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.