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:
How/why is this possible?
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