Ethan Shoe
Ethan Shoe

Reputation: 564

Trying to store string[][] in settings of WinForms application

I have a winforms application where I am trying to store a two-dimensional array to my Properties.settings to pull from later. I found out I can make a two-dimensional array in the settings via this code:

<Setting Name="TagPresets" Type="System.String[][]" Scope="User">
  <Value Profile="(Default)" />
</Setting>

This is great but whenever I try to programmatically store an array to it, I get this error:

Cannot implicitly convert type 'string[][*,*]' to 'System.Collections.Specialized.StringCollection'

This should not be happening since I obviously declared the setting to be a two-dimensional array. Here is the code where I'm trying to set it:

Settings.Default.TagPresets = new string[1][,] { new string[,] { { "", "" } } };

Any help or thoughts on another way to accomplish this would be appreciated.

Upvotes: 0

Views: 81

Answers (1)

TimB
TimB

Reputation: 1000

The settings cannot handle arrays, lists, or the like. The only list-like datatype they can handle is aforementioned StringCollection.

That said, the best way would probably be not to use settings at all, but a separate XML or JSON file which the program reads on launch and writes before exit.

While converting a StringCollection to an array, a list, a queue or a stack works, I do not think that this works for a multi-dimensional array.

As an afterthought: You could, of course, take each sub array in your main array, join it on a certain character, than add that joined string to the StringCollection. On next launch, iterate over the values of the StringCollection, split each on the character you used for join, and add the resulting arrays to a main array you created before. But I honestly would work with XML or JSON.

Upvotes: 2

Related Questions