[RESOLVED] Storing string in a settings string array
I have a few string arrays in the settings menu, and I need to add to those string arrays and save them depending on how many a person adds to it.
Executing the following code
Code:
Dim dictName(5) As String
Dim dNumWords(5) As Long
Dim dSize(5) As Long
Dim dPath(5) As String
Dim dDesc(5) As String
' Example of a branch
dName(0) = "Name"
dNumWords(0) = 1
dSize(0) = 4
dPath(0) = "Here"
dDesc(0) = "Hello."
' The next 5 branches would add the next values of the values in the array
'dName(1) = "Hi"
'..... etc. etc.
For i As Integer = 0 To 5 ' For 6 default dictionaries
My.Settings.dName(i) = dictName(i) ' I get a nullreference exception here
My.Settings.dNumWords(i) = dictNumWords(i).ToString() ' I get a nullreference exception here
My.Settings.dSize(i) = dictSize(i).ToString()
My.Settings.dPath(i) = dictPath(i)
My.Settings.dDesc(i) = dictDesc(i)
Next
I presume I get the following null reference exceptions because the System.Collections.Specialized.StringCollections string array in settings isn't instantiated.
The thing is, I don't know how to instantiate it and I don't know what the size will be in the end. A user can either add or remove to the size of the array.
I heard I can use ArrayLists, but I never used those.
Re: Storing string in a settings string array
A StringCollection is not an array. It's a collection, so what size it will be in the end is irrelevant. The point of collections in general is to provide dynamic array-like behaviour, i.e. they can be indexed like an array but they can grow and shrink dynamically.
As for instantiation, each of your settings is a property like any other and StringCollection is a class like any other so you set the property with a new object the same way as you would in any other case, i.e. create a new instance of the class by invoking a constructor and assign it to the property, e.g.
vb.net Code:
My.Settings.dName = New Specialized.StringCollection
Of course, you'll only want to do that if the property actually is Nothing to begin with:
vb.net Code:
If My.Settings.dName Is Nothing Then
Re: Storing string in a settings string array
That said, there's another - probably better - way around this. The way StringCollections are handled in the settings designer is a bit quirky. If you create a setting of that type on the Settings page the notice that the Value field is empty by default. If you click the button at the right of that field to open the editor, then add an item and click OK, notice that the Value field contains more than just that item. It actually contains a snippet of XML, which is what gets stored in the config file for that setting.
If you now open the editor again and delete that item and save again, notice that the item is gone but the XML around it remains. If you run the project now, you'll find that, instead of being Nothing, that setting will now refer to an empty StringCollection object. That means that you don't need any code to check whether it's Nothing or create a StringCollection object.
Re: Storing string in a settings string array
Okay, I actually just needed that clarification. I was adding the items like an array where I would add by index instead of using the .Add() function.