Serialize/DeSerialize a nested Class

D

davebaranas

I am able to serialize this but I get a null exception when I try to
deserialize it back

Even if I don't make any child classes it throws "Object reference not
set to an instance of an object."

I must be missing something here

Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdLoad.Click

Dim oProject As New Project
Dim oBuildings As New Project.Buildings
Dim oStoreys As New Project.Buildings.Storeys
Dim oSlabs As New Project.Buildings.Storeys.Slabs

oProject.Add(oBuildings)
Dim oBuilding As New Building
oBuildings.Add(oBuilding)

oBuilding.Add(oStoreys)
Dim oStorey As New Storey
oStoreys.Add(oStorey)

oStorey.Add(oSlabs)
Dim oSlab As New Slab
oSlabs.Add(oSlab)

Dim bf As New Formatters.Binary.BinaryFormatter()
Dim fs As New IO.FileStream("c:\Projects.dat",
IO.FileMode.Create)
bf.Serialize(fs, oProject)
fs.Close()
fs = Nothing
bf = Nothing

Dim bf1 As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim fs1 As New IO.FileStream("c:\Projects.dat",
IO.FileMode.Open)
oProject = DirectCast(bf.Deserialize(fs1), Project)
fs1.Close()
fs1 = Nothing
bf1 = Nothing

End Sub
________________________________________________________

<Serializable()> Public Class Project
Inherits System.Collections.CollectionBase

Public Function Add(ByVal oBuildings As Buildings) As Boolean
Try
List.Add(oBuildings)
Return True
Catch E As System.Exception
Return False
End Try
End Function
<Serializable()> Public Class Buildings
Inherits System.Collections.CollectionBase

Public Function Add(ByVal oBuilding As Building) As Boolean
Try
List.Add(oBuilding)
Return True
Catch E As System.Exception
Return False
End Try
End Function
<Serializable()> Public Class Storeys
Inherits System.Collections.CollectionBase

Public Function Add(ByVal oStorey As Storey) As Boolean
Try
List.Add(oStorey)
Return True
Catch E As System.Exception
Return False
End Try
End Function
<Serializable()> Public Class Slabs
Inherits System.Collections.CollectionBase
Public Function Add(ByVal oSlab As Slab) As Boolean
Try
List.Add(oSlab)
Return True
Catch E As System.Exception
Return False
End Try
End Function
End Class
End Class
End Class
End Class
<Serializable()> Public Class Building
Inherits System.Collections.CollectionBase

Private m_iRefNo As Integer
Private m_sName As String
Public Property RefNo() As Integer
Get
Return m_iRefNo
End Get
Set(ByVal value As Integer)
m_iRefNo = value
End Set
End Property
Public Property Name() As String
Get
Return m_sName
End Get
Set(ByVal Value As String)
m_sName = Value
End Set
End Property
Public Function Add(ByVal oStoreys As Project.Buildings.Storeys)
As Boolean
Try
List.Add(oStoreys)
Return True
Catch E As System.Exception
Return False
End Try
End Function
End Class
<Serializable()> Public Class Storey
Inherits System.Collections.CollectionBase

Private m_iRefNo As Integer
Private m_sName As String
Public Property RefNo() As Integer
Get
Return m_iRefNo
End Get
Set(ByVal Value As Integer)
m_iRefNo = Value
End Set
End Property
Public Property Name() As String
Get
Return m_sName
End Get
Set(ByVal Value As String)
m_sName = Value
End Set
End Property
Public Function Add(ByVal oWalls As
Project.Buildings.Storeys.Slabs) As Boolean
Try
List.Add(oWalls)
Return True
Catch E As System.Exception
Return False
End Try
End Function
End Class
<Serializable()> Public Class Slab

Private m_iRefNo As Integer
Private m_sName As String
Public Property RefNo() As Integer
Get
Return m_iRefNo
End Get
Set(ByVal Value As Integer)
m_iRefNo = Value
End Set
End Property
Public Property Name() As String
Get
Return m_sName
End Get
Set(ByVal Value As String)
m_sName = Value
End Set
End Property
End Class
 
D

davebaranas

I am able to serialize this but I get a null exception when I try to
deserialize it back
I had a typo with bf1 not being bf in the deserializer that fixed the
exception from being thrown

Got it working by adding a ref to the parent class in the child
class(s) constructors. Without this it won't deserialize correctly
even though it steps through it

e.g.
<Serializable()> Public Class Slab

Private m_iRefNo As Integer
Private m_sName As String
Private ReadOnly m_Storey As Storey
Public Property RefNo() As Integer
Get
Return m_iRefNo
End Get
Set(ByVal Value As Integer)
m_iRefNo = Value
End Set
End Property
Public Property Name() As String
Get
Return m_sName
End Get
Set(ByVal Value As String)
m_sName = Value
End Set
End Property
Public Sub New(ByVal Storey As Storey)
m_Storey = Storey
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top