0

I have created a new user setting called TheList as a System.Collections.Specialized.StringDictionary

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If My.Settings.TheList Is Nothing Then
        My.Settings.TheList = New System.Collections.Specialized.StringDictionary
    End If

    My.Settings.TheList.Add("Not", "Working")
    My.Settings.Save()
End Sub

However, for some reason My.Settings.TheList is actually always "nothing" and my dictionary entry is never saved.

Andy
  • 51
  • 4
  • FWIW, `StringDictionary` is a fairly early (in .NET terms) way of doing this - since generics have been around, it would be [more normal to use a `Dictionary(Of String, String)`](http://stackoverflow.com/questions/627716/stringdictionary-vs-dictionarystring-string). – James Thorpe Sep 20 '15 at 10:16
  • Look in the Output window or use Debug > Exceptions > tick CLR exceptions. The FileNotFoundException you see is normal. The InvalidOperationException is not, StringDictionary is not serializable. A generic Dictionary is not either. XML serialization is just rather lame. – Hans Passant Sep 20 '15 at 10:36
  • @JamesThorpe When working with VB.NET settings, that's [what you get](http://i.stack.imgur.com/w83v3.png). There is 'Browse...', but you cannot browse for a generic type. – GSerg Sep 20 '15 at 10:37
  • If you put that code somewhere else, like a button click you will get a `ConfigurationException: Configuration system failed to initialize`. Settings works best on simple types and you cannot add any type, esp Dictionaries, to it. Create a sensible type and serialize it yourself. – user3697824 Sep 20 '15 at 17:00

1 Answers1

0

It turns out that saving a dictionary just isn't supported. Instead, I decided to serialize it on my own using the code in this answer. Works like a dream.

Community
  • 1
  • 1
Andy
  • 51
  • 4