0

I am trying to save a list variable to My.Settings, but I cannot find a type that works. I am using Visual Studio 2017. Here is how I built the list...

Dim Parameters As New List(Of String)
Parameters.Add("Item1")
Parameters.Add("Item2")

The error comes up when trying to load or save the setting.

Dim Times As New List(Of String)
Times = My.Settings.Times

or

My.Settings.Item("Times") = Times

though the latter doesn't show up as an error but it crashes the program.

I have tried setting the type of setting to String, StringCollection, ListDictionary, etc. but to no avail.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Sinusoid
  • 31
  • 3
  • 2
    Please review the FAQ for help on how to ask a question: https://stackoverflow.com/help/how-to-ask – Jacob M. May 12 '18 at 22:27
  • I have read over that page. I have looked for answers to this question on here and other sites, and have not found an answer. There is no code to post or really any further details, it is a straight forward question so I kept it simple. – Sinusoid May 12 '18 at 22:31
  • 1
    The Stack Overflow community expects that you have done at least some effort to resolve this yourself and post examples (code). You say "there is no code," there should be. You mentioned having a list, post the code that produced that list. Then Google saving to my.settings, code samples come up immediately. Then try one of those. If you have problems, post the code from your attempt and someone will usually be happy to help. – Jacob M. May 12 '18 at 22:34
  • The second paragraph of [How to ask](https://stackoverflow.com/help/how-to-ask) explicitly states: _"Search, and research ...and keep track of what you find. Even if you **don't** find a useful answer elsewhere on the site, **including links** to related questions that **haven't helped** can help others in understanding **how your question is different from the rest.**"_ – Visual Vincent May 12 '18 at 22:41
  • It also says "Pretend you're talking to a busy colleague and have to sum up your entire question in one sentence:..." So I summed it up in a single simple sentence. It doesn't matter how I built the list, I have a list, the question concerns how to save the list to the user settings (my.settings). I know how to save data out to user settings, but cannot find how to save a list there, hence the question. The issue seems to be with the type I am selecting, so that is what I asked. – Sinusoid May 12 '18 at 22:45
  • Fun fact: StackOverflow *is* a repository of programming question and answers making it one of those places to search as pasrt of ***search and research**. In this case, there are 92,000 entries here on the topic – Ňɏssa Pøngjǣrdenlarp May 12 '18 at 23:14
  • Responding to your comment for future reference: _`It also says -snip- So I summed it up in a single simple sentence.`_ - The _problem_ may be summed up in a single sentence, but preferably not the entire question (what they actually meant is that you should try to keep it brief, but it doesn't have to be _exactly_ one sentence). The reason we want (and need) to see your research **and/or** your attempts is because it helps narrow down the problem even further (and because we want to see that you've actually tried something, as this is not a coding service). Help us help you. – Visual Vincent May 12 '18 at 23:40
  • _`It doesn't matter how I built the list`_ - We needed to see how you construct it so that we'd know _**what type of list**_ you're using (there are quite a few different types of lists/collections in the framework) and also _**what type of data it contains**_ (since, as I assume you well know, lists can contain other types than strings). This information is necessary for us to have in order to write an answer that suits your needs. Otherwise it might just have turned into a wild goose chase. ;) – Visual Vincent May 12 '18 at 23:47

1 Answers1

5

A System.Collections.Specialized.StringCollection is the most common one used, although you cannot assign a List(Of String) to it directly as they are different types.

Instead you have to iterate your List(Of String) and add every item to the My.Settings list:

'Remove the old items.
My.Settings.Clear()

'Add the new ones.
For Each Item As String In Times
    My.Settings.Times.Add(Item)
Next

Likewise, when reading it back into the List(Of String):

For Each Item As String In My.Settings.Times
    Times.Add(Item)
Next
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75