|
-
Feb 21st, 2003, 04:13 PM
#1
Thread Starter
Fanatic Member
Serialization Collections?
Hi
Is it possible to serialize collections variables?
I get the following error on
oBinaryFormatter.Serialize(oFileStream, _oCollection)
variables are declare like
Dim oBinFormatter As BinaryFormatter
Dim oFileStream As FileStream
Dim _oCollection As Collection = New Collection()
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
Additional information: The type Microsoft.VisualBasic.Collection in Assembly Microsoft.VisualBasic, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a is not marked as serializable.
-
Feb 21st, 2003, 04:35 PM
#2
The collection object is not serializeable so no. You'd have to create a custom collection that is serializable or loop through the collection and serialize each individual object instead.
-
Feb 21st, 2003, 05:19 PM
#3
Thread Starter
Fanatic Member
Thanks but im still having problems....
I get the following error
n unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
Additional information: The type WindowsApplication1.Form1+mystruct in Assembly WindowsApplication1, Version=1.0.1147.39837, Culture=neutral, PublicKeyToken=null is not marked as serializable.
VB Code:
Structure mystruct
Dim Header() As String
Dim Data() As Byte
End Structure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim struct(10) As mystruct
Dim h(5) As String
Dim d(10) As Byte
'-- Data 1
h(0) = "Header 1"
h(2) = "Header 2"
h(3) = "Header 3"
d(0) = Asc("W")
d(1) = Asc("O")
d(2) = Asc("R")
d(3) = Asc("K")
d(4) = Asc("!")
'-- Add Data 1 to struct
struct(0).Header = h
struct(0).Data = d
'-- Data 2
h(0) = "Header 4"
h(2) = "Header 5"
h(3) = "Header 6"
d(0) = Asc("N")
d(1) = Asc("O")
d(2) = Asc("W")
d(3) = Asc("!")
'-- Add Data 1 to struct
struct(1).Header = h
struct(1).Data = d
Dim oFileStream As IO.FileStream
Dim oBinaryFormatter As Runtime.Serialization.Formatters.Binary.BinaryFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
oFileStream = IO.File.Create("c:\b.txt")
oBinaryFormatter.Serialize(oFileStream, struct)
oFileStream.Close()
My data is structered like above ( but mass amounts of it ), and i just want to easily serialize the lot into a file, but i cant due to that error.
Am i doing this wrong?
-
Feb 21st, 2003, 05:26 PM
#4
New Member
You need to add the serializable attribute to your struct, like this
VB Code:
<serializable()> Structure mystruct
Dim Header() As String
Dim Data() As Byte
End Structure
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|