Mind Boggling Serialization Problem

bergy
01-05-2004, 09:33 PM
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:

Dim note As Object = deserializer.Deserialize(myFileStream)

My Load Sub:

Public Sub Load()
If Me.FileLocation Is Nothing Then
MsgBox("Filelocation not set to load.")
Else
Dim i As Integer
Dim deserializer As New BinaryFormatter()
Dim myFileStream As Stream = File.OpenRead(Me.FileLocation)
Dim tempNote As New Notebook()
Dim ex As Exception
Dim note As Object = deserializer.Deserialize(myFileStream)
'ERRORS RIGHT UP THERE ^^^^
tempNote = CType(note, Notebook)
myFileStream.Close()
Me.Name = tempNote.Name
Me.FileLocation = tempNote.FileLocation
End If
End Sub

My Save Sub:

Public Sub Save()
If Me.FileLocation Is Nothing Then
MsgBox("Filelocation not set to save.")
Else
Dim myFileStream As Stream =File.Create(Me.FileLocation)
Dim serializer As New BinaryFormatter()
serializer.Serialize(myFileStream, Me)
myFileStream.Close()
End If
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.
Public Sub New()

End Sub

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

reboot
01-06-2004, 04:43 AM
What exactly is the point of an empty constructor? You aren't loading the values back into the class on deserialization.

<Serializable()> Public Class Class1

Private x As Integer
Private y As Integer

'Simple constructor to load in values for the fields.
Public Sub New(ByVal argx As Integer, ByVal argy As Integer)
Me.x = argx
Me.y = argy
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum