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

Serializing custom component with a strongly typed collection property

P: n/a
I make a similar post in another newsgroup, but this example is
simplified somewhat.

I have a component (cmpMyComponent) with 2 properties. The 1st property
is a string value (Description) and the 2nd property is a strongly typed
collection class (myCollectionProperty).

The collection contains a simple class (myCustomClass) which has 1 text
property (TextProperty).

Editing the properties for the component in the windows forms designer
works fine, but compiling the application causes the collection property
to lose its values. I assume this is because the component's collection
property isn't being serialized to the the myForm.resx file
automatically.

I've tried setting a <serializable> attribute on the component, as well
as the
<DesignerSerializationVisibility(DesignerSerializa tionVisibility.Content
)> attribute on the collection property in the component.

Below is the sample project code. Help is greatly appreciated.

'// THIS IS THE COMPONENT

Public Class myComponent
Inherits System.ComponentModel.Component
Private m_strDescription As String
Private m_myCollectionProperty As myCustomClassCollection

#Region " Component Designer generated code "
Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()
'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub

Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
<DesignerSerializationVisibility(DesignerSerializa tionVisibility.Content
)> _
Public Property myCollectionProperty() As
myCustomClassCollection
Get
Return m_myCollectionProperty
End Get
Set(ByVal Value As myCustomClassCollection)
m_myCollectionProperty = Value
End Set
End Property

Public Property Description() As String
Get
Return m_strDescription
End Get
Set(ByVal Value As String)
m_strDescription = Value
End Set
End Property

End Class


'// THIS IS THE COLLECTION AND CUSTOM CLASS

<Serializable()> _
Public Class myCustomClassCollection
Inherits System.Collections.CollectionBase

Public Sub New()
MyBase.New()
End Sub

Public Sub New(ByVal myCValue As myCustomClassCollection)
MyBase.New()
Me.AddRange(myCValue)
End Sub

Public Sub New(ByVal myCValue() As myCustomClass)
MyBase.New()
Me.AddRange(myCValue)
End Sub

Default Public Property Item(ByVal intIndex As Integer) As
myCustomClass
Get
Return CType(List(intIndex), myCustomClass)
End Get
Set(ByVal Value As myCustomClass)
List(intIndex) = value
End Set
End Property

Public Function Add(ByVal myCValue As myCustomClass) As Integer
Return List.Add(myCValue)
End Function

Public Overloads Sub AddRange(ByVal myCValue() As myCustomClass)
Dim intCounter As Integer = 0
Do While (intCounter < myCValue.Length)
Me.Add(myCValue(intCounter))
intCounter = (intCounter + 1)
Loop
End Sub

Public Overloads Sub AddRange(ByVal myCValue As
myCustomClassCollection)
Dim intCounter As Integer = 0
Do While (intCounter < myCValue.Count)
Me.Add(myCValue(intCounter))
intCounter = (intCounter + 1)
Loop
End Sub

Public Function Contains(ByVal myCValue As myCustomClass) As Boolean
Return List.Contains(myCValue)
End Function

Public Sub CopyTo(ByVal myCArray() As myCustomClass, ByVal intIndex
As Integer)
List.CopyTo(myCArray, intIndex)
End Sub

Public Function IndexOf(ByVal myCValue As myCustomClass) As Integer
Return List.IndexOf(myCValue)
End Function

Public Sub Insert(ByVal intIndex As Integer, ByVal myCValue As
myCustomClass)
List.Insert(intIndex, myCValue)
End Sub

Public Shadows Function GetEnumerator() As myCustomClassEnumerator
Return New myCustomClassEnumerator(Me)
End Function

Public Sub Remove(ByVal myCValue As myCustomClass)
List.Remove(myCValue)
End Sub
End Class

Public Class myCustomClassEnumerator
Inherits Object
Implements System.Collections.IEnumerator

Private iEnBase As System.Collections.IEnumerator

Private iEnLocal As System.Collections.IEnumerable

Public Sub New(ByVal myCMappings As myCustomClassCollection)
MyBase.New()
Me.iEnLocal = CType(myCMappings, System.Collections.IEnumerable)
Me.iEnBase = iEnLocal.GetEnumerator
End Sub

Public ReadOnly Property Current() As myCustomClass
Get
Return CType(iEnBase.Current, myCustomClass)
End Get
End Property

ReadOnly Property System_Collections_IEnumerator_Current() As Object
Implements System.Collections.IEnumerator.Current
Get
Return iEnBase.Current
End Get
End Property

Public Function MoveNext() As Boolean
Return iEnBase.MoveNext
End Function

Function System_Collections_IEnumerator_MoveNext() As Boolean
Implements System.Collections.IEnumerator.MoveNext
Return iEnBase.MoveNext
End Function

Public Sub Reset()
iEnBase.Reset()
End Sub

Sub System_Collections_IEnumerator_Reset() Implements
System.Collections.IEnumerator.Reset
iEnBase.Reset()
End Sub
End Class

Public Class myCustomClass

Private m_strTextProperty As String

Public Property TextProperty() As String
Get
Return m_strTextProperty
End Get
Set(ByVal Value As String)
m_strTextProperty = Value
End Set
End Property

End Class

================
Jordan Bowness
================

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.