Serializing custom properties on a BindingList.

M

Monty

I've created an object (called cMyBindingList) that inherits from
BindingList. I can serialize it and it's child items with no problem.
However, I've also created a public variable in cMyBindingList that I set
when I first create it. This variable, even though it is public, does not
serialize to the XML. What is the reason for this? Is it possible to do it?
TIA for any light you can shed. Here's the sample code (from a form with a
single button on it):

Imports System.ComponentModel

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oList1 As New cMyBindingList(15) '<-- note that iType is
specified
For i As Integer = 1 To 5 'Load up list
Dim oItem As New cMyItem
oItem.Value1 = i.ToString
oItem.Value2 = i.ToString
oList1.Add(oItem)
Next
Dim oSerializer As New
Xml.Serialization.XmlSerializer(GetType(cMyBindingList))
Dim myWriter As System.IO.StreamWriter = New
System.IO.StreamWriter("C:\MyTest.xml")
oSerializer.Serialize(myWriter, oList1)
End Sub
End Class

<Serializable()> <System.Runtime.InteropServices.ComVisible(False)> _
Public Class cMyBindingList
Inherits BindingList(Of cMyItem)
Public miMyType As Integer '<-- THIS DOES NOT SERIALIZE!
Public Sub New() 'this parameterless constructor required for
deserialization
End Sub
Public Sub New(ByVal iType As Integer)
miMyType = iType
End Sub
End Class

<Serializable()> <System.Runtime.InteropServices.ComVisible(False)> _
Public Class cMyItem
Private msValue1 As String
Private msValue2 As String
Public Property Value1() As String
Get
Return msValue2
End Get
Set(ByVal value As String)
msValue2 = value
End Set
End Property
Public Property Value2() As String
Get
Return msValue1
End Get
Set(ByVal value As String)
msValue1 = value
End Set
End Property
End Class
 
L

Luke Zhang [MSFT]

Hello Monty,

BindingList class will be serialized as an array, its properties won't be
serialized. You may create a new class, add all customized properties in
this class, and then and cMyBindingList as a property of this new class.

Luke Zhang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
M

Monty

Thanks for the response. Are there any collection-type objects that will
serialize both public properties on the container and the object's child
items?
 
L

Luke Zhang [MSFT]

I have to say there is a limitation here. The work around is to create a
parent class and move all public properties into the parent class.

Luke Zhang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top