How can I store an array in my.settings in vb.net

1.1k views Asked by At

I am trying to store a user input array in a my.settings variable in vb.net. I would like for the user to enter an array in the form {1,2,3}, store that as a string in the setting and then be able to use the setting value later to create a new array. The code will be something like:

Dim inputarray()
Dim outputarray()

inputarray=textbox1.text
my.settings.inputstoredarray.add(inputarray)

outputarray=my.settings.inputstoredarray
textbox2.text=outputarray(0)

'If the user types "{1,2,3}' in textbox1, textbox2 should show "1"

I have tried multiple versions of this but there seem to always be type conversion errors. I don't understand why it works if I hardcode:

inputarray={1,2,3}

and yet the below code does not work:

inputarray=my.settings.inputstoredarray

How can I store a user provided array in my.settings and retrieve it for use later?

does not work even if I go into settings and set the string value for the setting to {1,2,3}

3

There are 3 answers

0
Mary On BEST ANSWER

Set up your setting in Project Properties, Settings tab as follows. enter image description here

Then save the setting as follows.

Private Sub SaveStringToSettings()
    My.Settings.StringOfInts = TextBox1.Text 'User types in {1, 2, 3}
End Sub

To retrieve the setting and turn it into an array

Private Sub CreateArrayFromSettings()
    Dim SettingValue = My.Settings.StringOfInts
    Debug.Print(SettingValue) 'View this in the Immediate window
    'Get rid of the braces
    Dim TrimmedString = SettingValue.Trim({"{"c, "}"c})
    'Split the string by the commas into an array 
    Dim Splits = TrimmedString.Split(","c)
    'Get rid of the spaces
    For i = 0 To Splits.Length - 1
        Splits(i) = Splits(i).Trim
    Next
    TextBox1.Text = Splits(0) 'Displays 1
End Sub
0
jmcilhinney On

You can't store an array in application settings. What you can do is create a setting of type StringCollection. You can then use it as a StringCollection in code or you can transfer the data back and forth between the StringCollection and an array if you really need an array.

Start by opening the Settings page of the project properties and creating a new setting of type System.Collections.Specialized.StringCollection. For this example, I'll name it MyStringCollection but you should name it appropriately for your app. When you do this, notice that the Value field is empty by default. This means that the setting is Nothing by default. That's OK but it means that you need to actually create the collection object in code before using it for the first time, e.g.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If My.Settings.MyStringCollection Is Nothing Then
        My.Settings.MyStringCollection = New StringCollection
    End If

    'Use My.Settings.MyStringCollection here.
End Sub

Alternatively, you can force the settings UI to create the object for you. To do that, select the Value field for that setting and click the browse (...) button. Add any arbitrary character and click the OK button. Notice that the Value field is populated with an XML snippet that contains the text you entered. Click the browse (...) button again, delete the text and click OK again. Notice that the XML remains, even though the text you had entered is removed. That XML will now create the StringCollection object automatically, so you don't need code to do so.

If you're happy wiith a collection of String values then you can use the setting directly in code wherever you like. It works, for the most part, like a List(Of String), allowing you to add and remove items at will. If you specifically need an array or you need a type other than String then you will have to do some translation, e.g.

'Load the collection items into a String array.
Dim myStringArray = My.Settings.MyStringCollection.Cast(Of String)().ToArray()

'Load the collection items into an Integer array.
Dim myIntegerArray = My.Settings.MyStringCollection.Cast(Of String)().Select(Function(s) CInt(s)).ToArray()

'Repopulate the collection from a String array.
My.Settings.MyStringCollection.Clear()
My.Settings.MyStringCollection.AddRange(myStringArray)

'Repopulate the collection from an Integer array.
My.Settings.MyStringCollection.Clear()
My.Settings.MyStringCollection.AddRange(myIntegerArray.Select(Function(n) n.ToString()).ToArray())

If you want to display the contents of your collection in a TextBox than you might do something like this:

TextBox1.Text = String.Join(",", My.Settings.MyStringCollection.Cast(Of String)())

That will create a single, comma-delimited String containing all the items. To repopulate the collection from a TextBox containing such text, do this:

My.Settings.MyStringCollection.Clear()
My.Settings.MyStringCollection.AddRange(TextBox1.Text.Split(","c))
0
SteveCinq On

You can browse for additional setting types in your Project properties/Settings tab but you'll find it fairly limited with the most common message being that you can't use that type. Array is one you can't use, I'm afraid.

But you can use the fully-supported StringCollection:

Dim saveTest As New StringCollection() From {"1", "2", "3", "4"}
My.Settings.MySetting = saveTest
My.Settings.Save()
Dim loadTest As StringCollection = My.Settings.MySetting

Another option would be to use, say, and XML or JSON string that you de-serialize but that's getting a bit involved.