Results 1 to 6 of 6

Thread: Mind Boggling Serialization Problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2004
    Location
    Troy, 0hi0
    Posts
    10

    Mind Boggling Serialization Problem

    When I try to load my Serialized object from a saved file it does this:

    -------------------------
    An unhandled exception of type
    'System.Runtime.Serialization.SerializationException' occurred in
    mscorlib.dll

    Additional information: The constructor to deserialize an object of
    type myNamespace.SmartObjects.Entry was not found.
    -------------------------

    Entry is another object that is called from the main object I'm trying to serialize. I have all my objects marked with <Serializable()>, it saves the object serialization flawlessly. The code errors here when I load it:

    VB Code:
    1. Dim note As Object = deserializer.Deserialize(myFileStream)

    My Load Sub:
    VB Code:
    1. Public Sub Load()
    2.             If Me.FileLocation Is Nothing Then
    3.                 MsgBox("Filelocation not set to load.")
    4.             Else
    5.                 Dim i As Integer
    6.                 Dim deserializer As New BinaryFormatter()
    7.                 Dim myFileStream As Stream = File.OpenRead(Me.FileLocation)
    8.                 Dim tempNote As New Notebook()
    9.                 Dim ex As Exception
    10.                 Dim note As Object = deserializer.Deserialize(myFileStream)
    11.                 'ERRORS RIGHT UP THERE ^^^^
    12.                 tempNote = CType(note, Notebook)
    13.                 myFileStream.Close()
    14.                 Me.Name = tempNote.Name
    15.                 Me.FileLocation = tempNote.FileLocation
    16.             End If
    17.         End Sub
    My Save Sub:
    VB Code:
    1. Public Sub Save()
    2.             If Me.FileLocation Is Nothing Then
    3.                 MsgBox("Filelocation not set to save.")
    4.             Else
    5.                 Dim myFileStream As Stream =File.Create(Me.FileLocation)
    6.                 Dim serializer As New BinaryFormatter()
    7.                 serializer.Serialize(myFileStream, Me)
    8.                 myFileStream.Close()
    9.             End If
    10.         End Sub

    I was told this:
    Serialization can freak out at times if an object doesn't have a default constructor. I mean if the object does not have a constructor that takes no parameters.
    So I added the below to all objects that didn't already have it. Still didn't work.
    VB Code:
    1. Public Sub New()
    2.  
    3. End Sub

    Could someone give me a specific answer? I'll post my entire namespace if i must.

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    For starters, I would wrap you code in a try catch block and do a e.tostring() to get the exact error message that is occurring.

    Does the class have any events? There are some issues with events and serialization too if that is the case.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2004
    Location
    Troy, 0hi0
    Posts
    10
    I did try Try and Catch and I received the same message that was listed above....

    e.String = "The constructor to deserialize an object of
    type myNamespace.SmartObjects.Entry was not found."

    I will post my whole class for you...

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2004
    Location
    Troy, 0hi0
    Posts
    10

    full source

    VB Code:
    1. #Region "  Notebook Objects"
    2.     <Serializable()> Public Class Notebook
    3.         'Inherits NoteSecurity
    4.         Private sName, sFileLocation As String
    5.         Public AutoNumberStyle As New AutoNumberStyle()
    6.         Public Entries As New EntryCollection()
    7.         Public NodeCol As New Entry()
    8.         Public Property Name() As String
    9.             Get
    10.                 Return sName
    11.             End Get
    12.             Set(ByVal Value As String)
    13.                 sName = Value
    14.             End Set
    15.         End Property
    16.         Public Property FileLocation() As String
    17.             Get
    18.                 Return sFileLocation
    19.             End Get
    20.             Set(ByVal Value As String)
    21.                 sFileLocation = Value
    22.             End Set
    23.         End Property
    24.         Public Sub Load()
    25.             If Me.FileLocation Is Nothing Then
    26.                 MsgBox("Filelocation not set to load.")
    27.             Else
    28.                 Dim i As Integer
    29.                 Dim deserializer As New BinaryFormatter()
    30.                 Dim myFileStream = New IO.FileStream(Me.FileLocation, IO.FileMode.Open) 'File.OpenRead(Me.FileLocation)
    31.                 Dim tempNote As New Notebook()
    32.                 Dim ex As Exception
    33.                 ' Try
    34.                 'Dim note As Object = deserializer.Deserialize(myFileStream)
    35.                 'tempNote = CType(note, Notebook)
    36.                 tempNote = deserializer.Deserialize(myFileStream)
    37.                 ' Catch ex
    38.                 ' MsgBox("An error occured while opening this file.", MsgBoxStyle.Critical, "SmarNote Error")
    39.                 '   Exit Sub
    40.                 'tempNote = CType(deserializer.Deserialize(myFileStream, Nothing), Notebook)
    41.                 ' End Try
    42.                 myFileStream.Close()
    43.                 Me.Name = tempNote.Name
    44.                 Me.FileLocation = tempNote.FileLocation
    45.             End If
    46.         End Sub
    47.         Public Sub Save()
    48.             If Me.FileLocation Is Nothing Then
    49.                 MsgBox("Filelocation not set to save.")
    50.             Else
    51.                 Dim myFileStream As Stream = File.Create(Me.FileLocation)
    52.                 Dim serializer As New BinaryFormatter()
    53.                 myFileStream.Seek(0, SeekOrigin.Begin)
    54.                 serializer.Serialize(myFileStream, Me)
    55.                 myFileStream.Close()
    56.             End If
    57.         End Sub
    58.         Public Sub New()
    59.  
    60.         End Sub
    61.     End Class
    62.  
    63.     <Serializable()> Public Class Entry
    64.         Inherits System.Windows.Forms.TreeNode
    65.         Private sName As String
    66.         Private nn As TreeNode
    67.  
    68.         Public Notes As New Page_Notes_Collection()
    69.         Public KeyPoints As New Page_KeyPoints_Collection()
    70.         Public ImpactWindows As New Page_ImpactWindow_Collection()
    71.         Public Caselaws As New Page_CaseLaw_Collection()
    72.         Public Property Name()
    73.             Get
    74.                 Return sName
    75.             End Get
    76.             Set(ByVal Value)
    77.                 sName = Value
    78.             End Set
    79.         End Property
    80.         Public Property Node() As TreeNode
    81.             Get
    82.                 Return nn
    83.             End Get
    84.             Set(ByVal Value As TreeNode)
    85.                 nn = Value
    86.             End Set
    87.         End Property
    88.         Public Sub New()
    89.  
    90.         End Sub
    91.     End Class
    92.  
    93.     <Serializable()> Public Class EntryCollection
    94.         Inherits System.Collections.CollectionBase
    95.         Public Sub Add(ByVal awidget As Entry)
    96.             List.Add(awidget)
    97.         End Sub
    98.         Public Sub Remove(ByVal index As Integer)
    99.             If index > Count - 1 Or index < 0 Then
    100.                 System.Windows.Forms.MessageBox.Show("Index not valid!")
    101.             Else
    102.                 List.RemoveAt(index)
    103.             End If
    104.         End Sub
    105.         Public ReadOnly Property Items(ByVal index As Integer) As Entry
    106.             Get
    107.                 Return CType(List.Item(index), Entry)
    108.             End Get
    109.         End Property
    110.         Public Sub New()
    111.  
    112.         End Sub
    113.     End Class
    114.     <Serializable()> Public Class Page_CaseLaw_Collection
    115.         Inherits System.Collections.CollectionBase
    116.         Public Sub Add(ByVal awidget As String)
    117.             List.Add(awidget)
    118.         End Sub
    119.         Public Sub Remove(ByVal index As Integer)
    120.             If index > Count - 1 Or index < 0 Then
    121.                 System.Windows.Forms.MessageBox.Show("Index not valid!")
    122.             Else
    123.                 List.RemoveAt(index)
    124.             End If
    125.         End Sub
    126.         Public ReadOnly Property Items(ByVal index As Integer) As String
    127.             Get
    128.                 Return CType(List.Item(index), String)
    129.             End Get
    130.         End Property
    131.         Public Sub New()
    132.  
    133.         End Sub
    134.     End Class
    135.     <Serializable()> Public Class Page_ImpactWindow_Collection
    136.         Inherits System.Collections.CollectionBase
    137.         Public Sub Add(ByVal awidget As String)
    138.             List.Add(awidget)
    139.         End Sub
    140.         Public Sub Remove(ByVal index As Integer)
    141.             If index > Count - 1 Or index < 0 Then
    142.                 System.Windows.Forms.MessageBox.Show("Index not valid!")
    143.             Else
    144.                 List.RemoveAt(index)
    145.             End If
    146.         End Sub
    147.         Public ReadOnly Property Items(ByVal index As Integer) As String
    148.             Get
    149.                 Return CType(List.Item(index), String)
    150.             End Get
    151.         End Property
    152.         Public Sub New()
    153.  
    154.         End Sub
    155.     End Class
    156.     <Serializable()> Public Class Page_Notes_Collection
    157.         Inherits System.Collections.CollectionBase
    158.         Public Sub Add(ByVal awidget As String)
    159.             List.Add(awidget)
    160.         End Sub
    161.         Public Sub Remove(ByVal index As Integer)
    162.             If index > Count - 1 Or index < 0 Then
    163.                 System.Windows.Forms.MessageBox.Show("Index not valid!")
    164.             Else
    165.                 List.RemoveAt(index)
    166.             End If
    167.         End Sub
    168.         Public ReadOnly Property Items(ByVal index As Integer) As String
    169.             Get
    170.                 Return CType(List.Item(index), String)
    171.             End Get
    172.         End Property
    173.         Public Sub New()
    174.  
    175.         End Sub
    176.     End Class
    177.     <Serializable()> Public Class Page_KeyPoints_Collection
    178.         Inherits System.Collections.CollectionBase
    179.         Public Sub Add(ByVal awidget As String)
    180.             List.Add(awidget)
    181.         End Sub
    182.         Public Sub Remove(ByVal index As Integer)
    183.             If index > Count - 1 Or index < 0 Then
    184.                 System.Windows.Forms.MessageBox.Show("Index not valid!")
    185.             Else
    186.                 List.RemoveAt(index)
    187.             End If
    188.         End Sub
    189.         Public ReadOnly Property Items(ByVal index As Integer) As String
    190.             Get
    191.                 Return CType(List.Item(index), String)
    192.             End Get
    193.         End Property
    194.         Public Sub New()
    195.  
    196.         End Sub
    197.     End Class
    198. #End Region

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Whats the AutoNumberStyle object?

    I think it is because the Entry object also has a TreeNode object as a property. I don't think a TreeNode is serializable which means that property can't be serialized. Why have a proeprty of an object that inherits from TreeNode be a TreeNode?

    Nevermind I spoke too soon. Although it definately has something to do with the TreeNode since if it doesn't inherit from it then it works fine.

    I came across this: http://www.dotnet247.com/247reference/msgs/6/31186.aspx

    The wierd thing is that you can serialize a TreeNode as a property just fine even if it has multiple sub nodes. You just can't serialize a class that derives from a TreeNode.
    Last edited by Edneeis; Jan 6th, 2004 at 04:04 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2004
    Location
    Troy, 0hi0
    Posts
    10

    thanks!

    Ed thanks so much for solving my weird issue, everything works out great now, thanks alot for the link!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width