Visual Basic: Store objects in a collection and serialize it to a file

Platform:Visual Basic
Task:Store objects in a collection and serialize it to a file
Discussion:
Store objects in a collection and serialize it to a file
Example:
'Here is the class for a Survey Object
<System.Serializable()>
Public Class Survey
    Public mUnit As String
    Public mStartDate As Date
    Public Sub New()

    End Sub

    Public Sub New(ByVal Unit As String, ByVal StartDate As Date)
        Me.mUnit = Unit
        Me.StartDate = StartDate
    End Sub

    Public Property Unit() As String
        Get
            Return mUnit
        End Get
        Set(ByVal Value As String)
            mUnit = Value
        End Set
    End Property

    Public Property StartDate() As String
        Get
            Return mStartDate
        End Get
        Set(ByVal Value As String)
            mStartDate = Value
        End Set
    End Property

    Public Function GetFilter() As String
        Return "Unit = '" & mUnit & "' and Start_Date = '" & mStartDate & "'"
    End Function
End Class

'Here is some code to create objects, serialize them to a file and retrieve them again
Imports System.IO


Public Class Form1
    Dim SurveysCollection As New Collection()
    Dim ObjectsFile As String = "C:\test.bin"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 1 To 11
            Dim mySurvey As New Survey(i, Now)
            SurveysCollection.Add(mySurvey)
        Next

        For Each SurveyItem As Survey In SurveysCollection
            OutputRichTextBox.AppendText("Adding object: " & SurveyItem.mUnit & " to SurveysCollection" & vbNewLine)
        Next       
    End Sub

    Private Sub SerializeToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SerializeToolStripButton.Click
        'serialize the object to file
        OutputRichTextBox.AppendText("Serializing to SurveysCollection" & vbNewLine)
        Dim fs As New FileStream(ObjectsFile, FileMode.OpenOrCreate)
        Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        bf.Serialize(fs, SurveysCollection)
        fs.Close()
    End Sub

    Private Sub DeserializeToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeserializeToolStripButton.Click
        ' Open file and deserialize to object again
        Dim fsRead As New FileStream(ObjectsFile, FileMode.Open)
        Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Dim objTest As Object = bf.Deserialize(fsRead)
        fsRead.Close()

        OutputRichTextBox.AppendText("Deserializing SurveysCollection" & vbNewLine)
        For Each SurveyItem As Survey In SurveysCollection
            OutputRichTextBox.AppendText("Deserializing object: " & SurveyItem.mUnit & " with filter '" & SurveyItem.GetFilter & "' to SurveysCollection" & vbNewLine)
        Next
    End Sub
End Class
Back to Index