0

I have a few arrays of strings in my settings labelled meas1, meas2, meas3 etc...

If I want to set the 6th item in each string collection to "", how would I do that? Below is my broken code of failed attempt:

for (int i = 19; i >= 0; i--)
{
    Properties.Settings.Default["meas" + i][5] = "";
}

I know I could do Properties.Settings.Default.meas1[5] = ""; but I want I have a lot of meas that I need to do so a for loop would be preferred.

J.Doe
  • 713
  • 1
  • 6
  • 19

1 Answers1

2

Maybe passing the item name and casting result to StringCollection would help:

for (int i = 19; i >= 0; i--)
{
    var prop = Properties.Settings.Default["meas" + i] as StringCollection;
    prop[5] = "";
}
Properties.Settings.Default.Save();

You need to replace as string[] with your exact data type. But the above solves your issue of accessing the item by name.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • can you expand a bit on what you mean by "You need to replace as string[] with your exact data type.". Isn't it string? – J.Doe Nov 30 '16 at 20:10
  • What is the exact type of your settings value ? Custom array of string ? Or a normal `StringCollection` ? – Zein Makki Nov 30 '16 at 20:13
  • Normal `StringCollection` – J.Doe Nov 30 '16 at 20:21
  • @J.Doe Do you have problems with the above edited answer ? – Zein Makki Nov 30 '16 at 20:32
  • error converting settings property to string collection :( Fixed it by removing properties so you're left with: `var prop = Properties.Settings.Default["meas" + i] as System.Collections.Specialized.StringCollection` – J.Doe Nov 30 '16 at 20:33
  • @J.Doe I just tried the above code and it works. Are you sure you defined the system settings are `System.Collections.Specialized.StringCollection`.. Can you edit your question and show how do you define your settings ? – Zein Makki Nov 30 '16 at 20:37
  • Yup work for me now just the properties was throwing it off – J.Doe Nov 30 '16 at 20:38