473,406 Members | 2,843 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,406 software developers and data experts.

Serializing a Structure

Does anyone know why this code gives an error:

<serializeable> public structure TestSerialize
anumber as integer
astring as string
end strucuture

Dim t As TestSerialize
Dim formatter As New BinaryFormatter
Dim s As New MemoryStream
t.anumber = 125
t.astring = "This is a test of Serialilzing a structure"
formatter.Serialize(s, t)
t = DirectCast(formatter.Deserialize(s), TestSerialize) ' This lines give
an error
saying that end of memory stream found before parsing was completed.

--
Dennis in Houston
Nov 21 '05 #1
2 1584
Why do you want to serialize to a memorystream and not a filestream? Here
are some example of each:

<Serializable()> Public Structure TestSerialize
Dim anumber As Integer
Dim astring As String
End Structure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' serialzie to memorystream
Dim t1 As TestSerialize
t1.anumber = 125
t1.astring = "This is a test of Serialilzing a structure"

Dim formatter As New BinaryFormatter

Dim ms As New MemoryStream
formatter.Serialize(ms, t1)

Dim t2 As TestSerialize
ms.Seek(0, SeekOrigin.Begin) ' think this was what you were missing

t2 = DirectCast(formatter.Deserialize(ms), TestSerialize)
ms.Close()

Debug.WriteLine(t2.anumber)
Debug.WriteLine(t2.astring)

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
' serialzie to filestream
Dim t1 As TestSerialize
t1.anumber = 125
t1.astring = "This is a test of Serialilzing a structure"

Dim formatter As New BinaryFormatter
Dim fs As New FileStream("c:\test.bin", FileMode.Create,
FileAccess.Write)

formatter.Serialize(fs, t1)
fs.Close()

Dim t2 As TestSerialize

fs = New FileStream("c:\test.bin", FileMode.Open, FileAccess.Read)
t2 = DirectCast(formatter.Deserialize(fs), TestSerialize)
fs.Close()

Debug.WriteLine(t2.anumber)
Debug.WriteLine(t2.astring)
End Sub

HTH,
Greg
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:B4**********************************@microsof t.com...
Does anyone know why this code gives an error:

<serializeable> public structure TestSerialize
anumber as integer
astring as string
end strucuture

Dim t As TestSerialize
Dim formatter As New BinaryFormatter
Dim s As New MemoryStream
t.anumber = 125
t.astring = "This is a test of Serialilzing a structure"
formatter.Serialize(s, t)
t = DirectCast(formatter.Deserialize(s), TestSerialize) ' This lines give
an error
saying that end of memory stream found before parsing was completed.

--
Dennis in Houston

Nov 21 '05 #2
Thanks a lot...that works.

Just to answer your question about serializing to memory instead of file:

I have a very complex structure containing reference objects that I want to
make a deep copy of from one variable to another. I currently have a
separate copy routine that does it but I was just looking for a simpler way
and thought that maybe I could serialize one variable to memor then
deserialize it to another thus making a keep copy of my original variable.

Thanks again for you help.

"Greg Burns" wrote:
Why do you want to serialize to a memorystream and not a filestream? Here
are some example of each:

<Serializable()> Public Structure TestSerialize
Dim anumber As Integer
Dim astring As String
End Structure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' serialzie to memorystream
Dim t1 As TestSerialize
t1.anumber = 125
t1.astring = "This is a test of Serialilzing a structure"

Dim formatter As New BinaryFormatter

Dim ms As New MemoryStream
formatter.Serialize(ms, t1)

Dim t2 As TestSerialize
ms.Seek(0, SeekOrigin.Begin) ' think this was what you were missing

t2 = DirectCast(formatter.Deserialize(ms), TestSerialize)
ms.Close()

Debug.WriteLine(t2.anumber)
Debug.WriteLine(t2.astring)

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
' serialzie to filestream
Dim t1 As TestSerialize
t1.anumber = 125
t1.astring = "This is a test of Serialilzing a structure"

Dim formatter As New BinaryFormatter
Dim fs As New FileStream("c:\test.bin", FileMode.Create,
FileAccess.Write)

formatter.Serialize(fs, t1)
fs.Close()

Dim t2 As TestSerialize

fs = New FileStream("c:\test.bin", FileMode.Open, FileAccess.Read)
t2 = DirectCast(formatter.Deserialize(fs), TestSerialize)
fs.Close()

Debug.WriteLine(t2.anumber)
Debug.WriteLine(t2.astring)
End Sub

HTH,
Greg
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:B4**********************************@microsof t.com...
Does anyone know why this code gives an error:

<serializeable> public structure TestSerialize
anumber as integer
astring as string
end strucuture

Dim t As TestSerialize
Dim formatter As New BinaryFormatter
Dim s As New MemoryStream
t.anumber = 125
t.astring = "This is a test of Serialilzing a structure"
formatter.Serialize(s, t)
t = DirectCast(formatter.Deserialize(s), TestSerialize) ' This lines give
an error
saying that end of memory stream found before parsing was completed.

--
Dennis in Houston


Nov 21 '05 #3

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

Similar topics

0
by: rgerla | last post by:
I'm trying to serialize a compount structure set for a web service, the sturctures are something like this: Public Structure UserPhone Public phonenumber As String Public phonetype As Char End...
10
by: copx | last post by:
I want to save a struct to disk.... as plain text. At the moment I do it with a function that just writes the data using fprintf. I mean like this: fprintf(fp, "%d %d", my_struct.a, my_struct.b)...
5
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
2
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: +...
0
by: olsonchris | last post by:
Hello all, I have what appears to be a simple question but after quite a bit of research I can't seem to find an answer. Basically, I am serializing a class into an XML document. This is...
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
2
by: Kurious Oranj | last post by:
I've currently got a database structure which is something like:- class a - member type - effective date and:- class b - client name
2
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hi, I know this will sound like a lot of hand waving, and I'll be glad to supply some sample code if necessary, but I thought something obvious might jump out at someone without doing so. ...
12
by: Cagdas Ozgenc | last post by:
Greetings, When directly serializing C++ structures to a file with the standard library functions giving the address of the data and length of structure using the sizeof operator, do I risk...
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
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
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.