PDA

Click to See Complete Forum and Search --> : Serialization Collections?


Geespot
Feb 21st, 2003, 03:13 PM
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.

Edneeis
Feb 21st, 2003, 03:35 PM
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.

Geespot
Feb 21st, 2003, 04:19 PM
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.


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?

DSAINMON
Feb 21st, 2003, 04:26 PM
You need to add the serializable attribute to your struct, like this


<serializable()> Structure mystruct
Dim Header() As String
Dim Data() As Byte
End Structure