Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 5th, 2004, 10:52 PM
bergy bergy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Troy 0hi0
Posts: 3 bergy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to bergy
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.

Reply With Quote
  #2  
Old January 6th, 2004, 03:26 AM
williamcrawley williamcrawley is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: uk
Posts: 91 williamcrawley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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?

Reply With Quote
  #3  
Old January 6th, 2004, 02:11 PM
bergy bergy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Troy 0hi0
Posts: 3 bergy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to bergy
The deserilize function is posted up there in the "Load" sub... i will post my whole namespace in a sec

Reply With Quote
  #4  
Old January 6th, 2004, 02:13 PM
bergy bergy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Troy 0hi0
Posts: 3 bergy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to bergy
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Mind Boggling Serialization Error


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT