Problem with persisting a collection, that references an internal property, at design time in winfor

Posted by Jules on Stack Overflow See other posts from Stack Overflow or by Jules
Published on 2010-03-11T18:21:17Z Indexed on 2010/03/11 19:14 UTC
Read the original article Hit count: 364

Filed under:
|
|
|
|

ETA: Jesus, I'm sick of this. This problem was specifically about persisting an interface collection but now on further testing it doesn't work for a normal collection. Here's some even simpler code:

Public Class Anger
End Class

Public Class MyButton
    Inherits Button

Private _Annoyance As List(Of Anger)
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public ReadOnly Property Annoyance() As List(Of Anger)
    Get
        Return _Annoyance
    End Get
End Property

Private _InternalAnger As Anger
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public ReadOnly Property InternalAnger() As Anger
    Get
        Return Me._InternalAnger
    End Get
End Property

Public Sub New()
    Me._Annoyance = New List(Of Anger)
    Me._InternalAnger = New Anger
    Me._Annoyance.Add(Me._InternalAnger)
End Sub

End Class

The designer screws up the persistence code in the same way as the original problem.

---- Original Problem

The easiest way to explain this problem is to show you some code:

Public Interface IAmAnnoyed
End Interface

Public Class IAmAnnoyedCollection
    Inherits ObjectModel.Collection(Of IAmAnnoyed)
End Class

Public Class Anger
    Implements IAmAnnoyed
End Class

Public Class MyButton
    Inherits Button

Private _Annoyance As IAmAnnoyedCollection
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public ReadOnly Property Annoyance() As IAmAnnoyedCollection
    Get
        Return _Annoyance
    End Get
End Property

Private _InternalAnger As Anger
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public ReadOnly Property InternalAnger() As Anger
    Get
        Return Me._InternalAnger
    End Get
End Property

Public Sub New()
    Me._Annoyance = New IAmAnnoyedCollection
    Me._InternalAnger = New Anger
    Me._Annoyance.Add(Me._InternalAnger)
End Sub

End Class

And this is the code that the designer generates:

Private Sub InitializeComponent()
    Dim Anger1 As Anger = New Anger
    Me.MyButton1 = New MyButton
    '
    'MyButton1
    '
    Me.MyButton1.Annoyance.Add(Anger1)
    // Should be: Me.MyButton1.Annoyance.Add(Me.MyButton1.InternalAnger)
    '
    'Form1
    '
    Me.Controls.Add(Me.MyButton1)

End Sub

I've added a comment to the above to show how the code should have been generated.

Now, if I dispense with the interface and just have a collection of Anger, then it persists correctly.

Any ideas?

© Stack Overflow or respective owner

Related posts about serialization

Related posts about .NET