472,362 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Serialization problem

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
2 1544
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David K | last post by:
we are having a problem when trying to serilalize and deserialize DataTable When replacing the container to be ListArray or HashTable everything works fine The code is very straight forward (...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
0
by: ktn | last post by:
Hi all, I'm a .NET beginner and I've got a problem on a program where I try to do an XML serialization. I get the following error : "An unmanaged exception of type...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
2
by: ofer | last post by:
Hi, I am working with the beta version of the new .net framework (Whidbey) and I encountered a problem with serialization that did'nt exist in the .net 2003 the situation is like this : I have...
0
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem...
5
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.