PDA

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


sunburnt
Mar 9th, 2002, 03:49 PM
Back to this :P

I've noticed that when I try to serialize a Private Collection, I recieve no error -- it just doesn't save correctly. Now, I tried this:

<Serializable()> Public Class Test
Public R As New Collection()
Public M As String
End Class

Private Sub SaveSettings(ByRef mypath As String, ByRef myObject As Object)
Dim XmlS As XmlSerializer
Dim fStream As IO.FileStream
'Try

XmlS = New XmlSerializer(myObject.GetType)
fStream = New IO.FileStream(mypath, IO.FileMode.Create)
XmlS.Serialize(fStream, myObject)
fStream.Close()
End Sub

'CODE
Dim f As New Test()
f.R.Add("hi")
f.m = ":-)"
SaveSettings("C:\f.xml", f)


Which finally throws an error.
System.InvalidOperationException: There was an error reflecting 'MoW.Test'. ---> System.InvalidOperationException: You must implement the Add(System.Object) method on Microsoft.VisualBasic.Collection because it inherits from ICollection.

What does this mean?

gijsj
Mar 10th, 2002, 06:59 AM
When a class implements the ICollection interface it must include an Add method and Item property to be Serializable.

sunburnt
Mar 10th, 2002, 09:35 AM
Right...so how would I go about doing that? :D

gijsj
Mar 10th, 2002, 10:30 AM
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemxmlserializationxmlserializerclasstopic.htm

sunburnt
Mar 10th, 2002, 10:44 AM
thanks for your help.