473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Serialize/DeSerialize a nested Class

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.Bin aryFormatter()
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

Sep 19 '06 #1
1 3629
On Mon, 18 Sep 2006 23:34:33 -0700, da*********@shaw.ca wrote:
>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
Sep 19 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
0
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
1
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
3
by: MAY | last post by:
Hi, I have a problem about serialize the form controls. I wrote a test program to test serialize a from but fail (->An unhandled exception of type...
1
by: Rick Luckwell | last post by:
I have 3 collections(reports, services, charts) of objects(report, service, chart) that are nested with each other. When I serialize the object the output only contains reports and services but...
2
by: Joe | last post by:
Hi I have a Generics List in a PropertyGrid I am able to Serialize it to XML but when I try to deserialize back to the class of the PropertyGrid The Constructor doesn't seem to fire to reload...
4
by: Hone | last post by:
I'm trying to serialize/deserialize the XML for an RSS 1.0 Feed (i.e. RDF) and the root element, as required by the standard, looks like this: <rdf:RDF ...> </rdf:RDF> However, I've tried...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
4
by: djidan | last post by:
hi, i am a newbe to c#, i'm trying to send en custom object from a client to a server, after reading a lot on the web ifound that i need to use: BinaryFormatter, MemoryStream, and Serialize...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.