J.Doe J.Doe - 1 year ago 69
C# Question

How to access an indexed string from a string collection saved in settings?

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.

Answer Source

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] = "";

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download