473,396 Members | 1,772 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,396 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 1598
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.