|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Mind Boggling Serialization Error
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. |
|
#2
|
|||
|
|||
|
Might be useful to see the code for Deserialize since the error is really occuring in here. As an aside, the file that you are trying to read presumably has read permissions?
|
|
#3
|
|||
|
|||
|
The deserilize function is posted up there in the "Load" sub... i will post my whole namespace in a sec
|
|
#4
|
|||
|
|||
|
Code:
#Region " Notebook Objects"
<Serializable()> Public Class Notebook
'Inherits NoteSecurity
Private sName, sFileLocation As String
Public AutoNumberStyle As New AutoNumberStyle()
Public Entries As New EntryCollection()
Public NodeCol As New Entry()
Public Property Name() As String
Get
Return sName
End Get
Set(ByVal Value As String)
sName = Value
End Set
End Property
Public Property FileLocation() As String
Get
Return sFileLocation
End Get
Set(ByVal Value As String)
sFileLocation = Value
End Set
End Property
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 = New IO.FileStream(Me.FileLocation, IO.FileMode.Open) 'File.OpenRead(Me.FileLocation)
Dim tempNote As New Notebook()
Dim ex As Exception
' Try
'Dim note As Object = deserializer.Deserialize(myFileStream)
'tempNote = CType(note, Notebook)
tempNote = deserializer.Deserialize(myFileStream)
' Catch ex
' MsgBox("An error occured while opening this file.", MsgBoxStyle.Critical, "SmarNote Error")
' Exit Sub
'tempNote = CType(deserializer.Deserialize(myFileStream, Nothing), Notebook)
' End Try
myFileStream.Close()
Me.Name = tempNote.Name
Me.FileLocation = tempNote.FileLocation
End If
End 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()
myFileStream.Seek(0, SeekOrigin.Begin)
serializer.Serialize(myFileStream, Me)
myFileStream.Close()
End If
End Sub
Public Sub New()
End Sub
End Class
<Serializable()> Public Class Entry
Inherits System.Windows.Forms.TreeNode
Private sName As String
Private nn As TreeNode
Public Notes As New Page_Notes_Collection()
Public KeyPoints As New Page_KeyPoints_Collection()
Public ImpactWindows As New Page_ImpactWindow_Collection()
Public Caselaws As New Page_CaseLaw_Collection()
Public Property Name()
Get
Return sName
End Get
Set(ByVal Value)
sName = Value
End Set
End Property
Public Property Node() As TreeNode
Get
Return nn
End Get
Set(ByVal Value As TreeNode)
nn = Value
End Set
End Property
Public Sub New()
End Sub
End Class
<Serializable()> Public Class EntryCollection
Inherits System.Collections.CollectionBase
Public Sub Add(ByVal awidget As Entry)
List.Add(awidget)
End Sub
Public Sub Remove(ByVal index As Integer)
If index > Count - 1 Or index < 0 Then
System.Windows.Forms.MessageBox.Show("Index not valid!")
Else
List.RemoveAt(index)
End If
End Sub
Public ReadOnly Property Items(ByVal index As Integer) As Entry
Get
Return CType(List.Item(index), Entry)
End Get
End Property
Public Sub New()
End Sub
End Class
<Serializable()> Public Class Page_CaseLaw_Collection
Inherits System.Collections.CollectionBase
Public Sub Add(ByVal awidget As String)
List.Add(awidget)
End Sub
Public Sub Remove(ByVal index As Integer)
If index > Count - 1 Or index < 0 Then
System.Windows.Forms.MessageBox.Show("Index not valid!")
Else
List.RemoveAt(index)
End If
End Sub
Public ReadOnly Property Items(ByVal index As Integer) As String
Get
Return CType(List.Item(index), String)
End Get
End Property
Public Sub New()
End Sub
End Class
<Serializable()> Public Class Page_ImpactWindow_Collection
Inherits System.Collections.CollectionBase
Public Sub Add(ByVal awidget As String)
List.Add(awidget)
End Sub
Public Sub Remove(ByVal index As Integer)
If index > Count - 1 Or index < 0 Then
System.Windows.Forms.MessageBox.Show("Index not valid!")
Else
List.RemoveAt(index)
End If
End Sub
Public ReadOnly Property Items(ByVal index As Integer) As String
Get
Return CType(List.Item(index), String)
End Get
End Property
Public Sub New()
End Sub
End Class
<Serializable()> Public Class Page_Notes_Collection
Inherits System.Collections.CollectionBase
Public Sub Add(ByVal awidget As String)
List.Add(awidget)
End Sub
Public Sub Remove(ByVal index As Integer)
If index > Count - 1 Or index < 0 Then
System.Windows.Forms.MessageBox.Show("Index not valid!")
Else
List.RemoveAt(index)
End If
End Sub
Public ReadOnly Property Items(ByVal index As Integer) As String
Get
Return CType(List.Item(index), String)
End Get
End Property
Public Sub New()
End Sub
End Class
<Serializable()> Public Class Page_KeyPoints_Collection
Inherits System.Collections.CollectionBase
Public Sub Add(ByVal awidget As String)
List.Add(awidget)
End Sub
Public Sub Remove(ByVal index As Integer)
If index > Count - 1 Or index < 0 Then
System.Windows.Forms.MessageBox.Show("Index not valid!")
Else
List.RemoveAt(index)
End If
End Sub
Public ReadOnly Property Items(ByVal index As Integer) As String
Get
Return CType(List.Item(index), String)
End Get
End Property
Public Sub New()
End Sub
End Class
#End Region
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Mind Boggling Serialization Error |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|