By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

Serializing custom properties on a BindingList.

P: n/a
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(cMyBinding List))
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
Mar 23 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.)

Mar 24 '06 #2

P: n/a
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?


"Luke Zhang [MSFT]" <lu******@online.microsoft.com> wrote in message
news:Zu**************@TK2MSFTNGXA01.phx.gbl...
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.)

Mar 27 '06 #3

P: n/a
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.)

Mar 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.