Link to home
Start Free TrialLog in
Avatar of RobertFromSecretWeapons
RobertFromSecretWeapons

asked on

How to Serialize / Deserialize a Collection Object

Hi,

Before I lose my mind here, what's wrong with this code.  The SaveCollection routine throws an exception saying that the class is not marked as Serializeable.  Nonsense.  The example in the book i got this from, doesn't even mark the class serializeable either.  I thought the whole point of using a Serializer function (bf.Serialize(fs, TheCollection)) was so that the arguments didn't need to be marked serializeable.  Anyhow, I marked that class as Serializable, and it still doesn't not work.

The LoadCollection routine can't be tested until there is a saved object first.

Please help before I resort to the "bottle"...
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

Public Class Class_TriggerDatabase

    <Serializable()> _
    Public Class Data

        Public Structure TriggerInfo
            Dim UpperTriggerAction As String
            Dim UT3 As String
            Dim UT2 As String
            Dim UT1 As String
            Dim LT1 As String
            Dim LT2 As String
            Dim LT3 As String
        End Structure

        Public Triggers As Collection

        Public Sub New()
            Triggers = New Collection
        End Sub
    End Class

    Const FileName As String = "TriggerData.dat"

    Dim TheCollection As Data

    Dim FullPathFilename As String

    Public Sub New(ByVal PathName As String)
        Try
            FullPathFilename = PathName & "\" & FileName
            LoadCollection()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub LoadCollection()
        Try
            Dim fs As FileStream = New FileStream(FullPathFilename, FileMode.Open)
            Dim bf As New BinaryFormatter()
            TheCollection = CType(bf.Deserialize(fs), Data)
            fs.Close()
        Catch ex As Exception
        End Try
    End Sub

    Public Sub SaveCollection()
        Try
            Dim fs As FileStream = New FileStream(FullPathFilename, FileMode.Create)
            Dim bf As New BinaryFormatter()
            bf.Serialize(fs, TheCollection)
            fs.Close()
        Catch ex As Exception
        End Try
    End Sub

    Public Function RetrieveTriggersForThisReportName(ByVal ReportName As String, ByRef TI As Data.TriggerInfo) As Boolean
        RetrieveTriggersForThisReportName = False
        Try
            If TheCollection.Triggers.Contains(ReportName) = True Then
                TI = DirectCast(TheCollection.Triggers.Item(ReportName), Data.TriggerInfo)
                RetrieveTriggersForThisReportName = True
            End If
        Catch ex As Exception
        End Try
    End Function

    Public Function SaveTriggersForThisReportName(ByVal ReportName As String, ByRef TI As Data.TriggerInfo) As Boolean
        SaveTriggersForThisReportName = False
        Try
            If TheCollection.Triggers.Contains(ReportName) Then
                TheCollection.Triggers.Remove(ReportName)
            End If
            TheCollection.Triggers.Add(TI, ReportName)
            SaveTriggersForThisReportName = True
        Catch ex As Exception
        End Try
    End Function
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RobertFromSecretWeapons
RobertFromSecretWeapons

ASKER

Hi Guys,

Well, you're both right.  

Idle_Mind, I've never seen ANY serialization example where a "double" serializeable attribute was needed.  But indeed, your suggestion did make a difference in this case. Stupid really.... Maybe it's because I am only serializing a portion of the class.

OmegaOmega, you are right too.  Before posting this thread, I was single stepping this code and saw the exceptions thrown even though there are print statements in the handlers.  I was actually initializing an empty collection early on, but somehow, that code branch got removed.

Thank-you both.