By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,144 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,144 IT Pros & Developers. It's quick & easy.

Serialization problem

P: n/a
Hiddeldi ho,

I want to save an object so that I can use its content after I restart
my program. I tried to solve this with serialization because someone
told me that this is the correct way for this.
So I wrote the following code to serialize\deserialize the object, but
now I have the problem that the object has a generic list and when I'm
trying to deserialize it, I get an error.

<System.Serializable()Public Class TestClass
Implements Runtime.Serialization.ISerializable
Private lstContent As List(Of Object)

Public Sub New(ByVal info As Serialization.SerializationInfo, ByVal
context As Serialization.StreamingContext) Implements
Serialization.ISerializable.GetObjectData
lstContent = CType(info.GetValue("MyList", lstContent.GetType),
List(Of Object))
End Sub

Public Sub GetObjectData(ByVal info As
Serialization.SerializationInfo, ByVal context As
Serialization.StreamingContext) Implements
Serialization.ISerializable.GetObjectData
info.addValue("MyList",lstContent)
End Sub

Public Sub Serialize()
Dim fs As New IO.FileStream("DataFile.dat", IO.FileMode.Create)
' Construct a BinaryFormatter and use it to serialize the data
to the stream.
Dim formatter As New
Runtime.Serialization.Formatters.Binary.BinaryForm atter
Try
formatter.Serialize(fs, Me)
Catch e As Runtime.Serialization.SerializationException
MessageBox.Show("Failed to serialize. Reason: " &
e.Message)
Throw
Finally
fs.Close()
End Try
End Sub

Public Shared Function Deserialize() As TestClass
Dim fs As New IO.FileStream("DataFile.dat", IO.FileMode.Open)
Try
Dim formatter As New
Runtime.Serialization.Formatters.Binary.BinaryForm atter

Return DirectCast(formatter.Deserialize(fs), TestClass)
Catch e As Runtime.Serialization.SerializationException
MessageBox.Show("Failed to deserialize. Reason: " &
e.Message)
Throw
Finally
fs.Close()
End Try
End Function

Can anybody give me an advice to solve my problem please?
Or does anybody know a simpler\better way to save an object?

Thanks,
Norman

Nov 30 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
It might be helpful to know what the error is that you're getting.

S

"Norman Chong" <no*********@freenet.dewrote in message
news:11*********************@l39g2000cwd.googlegro ups.com...
Hiddeldi ho,

I want to save an object so that I can use its content after I restart
my program. I tried to solve this with serialization because someone
told me that this is the correct way for this.
So I wrote the following code to serialize\deserialize the object, but
now I have the problem that the object has a generic list and when I'm
trying to deserialize it, I get an error.

<System.Serializable()Public Class TestClass
Implements Runtime.Serialization.ISerializable
Private lstContent As List(Of Object)

Public Sub New(ByVal info As Serialization.SerializationInfo, ByVal
context As Serialization.StreamingContext) Implements
Serialization.ISerializable.GetObjectData
lstContent = CType(info.GetValue("MyList", lstContent.GetType),
List(Of Object))
End Sub

Public Sub GetObjectData(ByVal info As
Serialization.SerializationInfo, ByVal context As
Serialization.StreamingContext) Implements
Serialization.ISerializable.GetObjectData
info.addValue("MyList",lstContent)
End Sub

Public Sub Serialize()
Dim fs As New IO.FileStream("DataFile.dat", IO.FileMode.Create)
' Construct a BinaryFormatter and use it to serialize the data
to the stream.
Dim formatter As New
Runtime.Serialization.Formatters.Binary.BinaryForm atter
Try
formatter.Serialize(fs, Me)
Catch e As Runtime.Serialization.SerializationException
MessageBox.Show("Failed to serialize. Reason: " &
e.Message)
Throw
Finally
fs.Close()
End Try
End Sub

Public Shared Function Deserialize() As TestClass
Dim fs As New IO.FileStream("DataFile.dat", IO.FileMode.Open)
Try
Dim formatter As New
Runtime.Serialization.Formatters.Binary.BinaryForm atter

Return DirectCast(formatter.Deserialize(fs), TestClass)
Catch e As Runtime.Serialization.SerializationException
MessageBox.Show("Failed to deserialize. Reason: " &
e.Message)
Throw
Finally
fs.Close()
End Try
End Function

Can anybody give me an advice to solve my problem please?
Or does anybody know a simpler\better way to save an object?

Thanks,
Norman

Nov 30 '06 #2

P: n/a

Steve Long wrote:
It might be helpful to know what the error is that you're getting.
Hi Steve
My deserialized object always had the value 'nothing'. But I already
solved the problem (I just forgot to post that - Sorry!)

Thanks,
Norman

Dec 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.