0

I'm aware that members of custom classes are subject to the same requirements as the class itself when they're supposed to be saved in My.Settings. One of those requirements is that the class - and all its members - have an empty constructor. Now, I need to save some font settings. Font does not have an empty constructor according to the documentation. Logically, my class containing Font type members cannot be saved to My.Settings.

So far, so bad. However, I did notice it is possible to save the value of a Font object directly to My.Settings for some reason. I have confirmed it's the same System.Drawing.Font I tried to use in my custom class.

My two questions are now:

  1. How/why is this possible?

  2. Given what I wrote about Font being a possible My.Setting, is there any way to have a member of type Font and save the class without creating my own implementation of Font [or something featuring the necessary properties]?

I've tried looking this up on Google, but to no avail. Documentation for My.Settings/serialisation didn't help much either.

Edit: Here's the code of my custom class:

<Serializable()> Public Class MyObject
Implements ISerializable

Private _name As String
Private _text As String
Private _font As Font

Public Property Name As String
    Get
        Return _name
    End Get
    Set(value As String)
        _name = value
    End Set
End Property

Public Property Text As String
    Get
        Return _text
    End Get
    Set(value As String)
        _text = value
    End Set
End Property

Public Property Font As Font
    Get
        Return _font
    End Get
    Set(value As Font)
        _font = value
    End Set
End Property

<NonSerialized()> Public Const CurrentDate As String = "something"
<NonSerialized()> Public Const DateSelection As String = "something else"
End Class

And here's the code of my keyed collection of custom class objects:

Imports System.Collections.ObjectModel

<Serializable()> Public Class MyCollection
Inherits KeyedCollection(Of String, MyObject)

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

Protected Overrides Function GetKeyForItem(item As MyObject) As String
    Return item.Name
End Function
End Class
Yuri
  • 1
  • 1
  • It _does_ implement `ISerializable` though, that may be related? – James Thorpe Jul 16 '15 at 16:27
  • It also has [a private constructor](http://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Advanced/Font.cs,83) that looks rather related. – James Thorpe Jul 16 '15 at 16:30
  • what does your custom class represent? – Ňɏssa Pøngjǣrdenlarp Jul 16 '15 at 17:04
  • @Plutonix: What do you mean by "what does your custom class represent"? In any case, I have added the source code to my custom class and the keyed collection of said class I was going to save it in [neither can be persisted]. – Yuri Jul 17 '15 at 11:57
  • Which are you trying to save in Settings, MyObject or the keyed collection? If the later, what is the Settings DataType? The issue nay not be the font. – Ňɏssa Pøngjǣrdenlarp Jul 17 '15 at 12:15
  • I tried both, the collection as a KeyedCollection [I have done this in another project and it worked]. I'm also pretty sure it's because of Font since that's the exception message I get when trying to save: "The Element 'PdfControl.MyObject.Font' of type 'System.Drawing.Font' can't be serialised." & "'System.Drawing.Font' can't be serialised because it is lacking a parametre-less constructor." [sorry if the wording isn't authentic; I translated that] – Yuri Jul 17 '15 at 12:37

0 Answers0