简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM