Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.Formatters.Soap

Public Class Form1

    Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click
        ' Create Bob and clone a copy of him into a file.
        Dim Bob As New SerializablePerson("Bob", 34, 5.25)
        Dim fs As New FileStream("c:\bob.dat", FileMode.Create)

        Dim f As System.Runtime.Serialization.IFormatter
        If optSoap.Checked = True Then
            f = New SoapFormatter()
        Else
            f = New BinaryFormatter()
        End If

        ' Store Bob with the help of the BinaryFormatter.
        f.Serialize(fs, Bob)

        fs.Close()

        ' Erase the current copy of Bob.
        Bob = Nothing

        ' Revive Bob by using the file, and check that his data is correct.
        fs = New FileStream("c:\bob.dat", FileMode.Open)

        ' Retrieve Bob with the help of the BinaryFormatter.
        Bob = CType(f.Deserialize(fs), SerializablePerson)

        ' Verify Bob's information.
        MessageBox.Show(Bob.Name)
        fs.Close()
    End Sub

End Class
