简体   繁体   English

如何从设置中保存的字符串集合中访问索引字符串?

[英]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... 我的设置中有一些字符串数组,分别标记为meas1,meas2,meas3等。

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] = ""; 我知道我可以做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. 但我想我有很多需要做的措施,因此首选for循环。

Maybe passing the item name and casting result to StringCollection would help: 将项目名称和转换结果传递给StringCollection可能会有所帮助:

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. 您需要用您的确切数据类型替换as string[] But the above solves your issue of accessing the item by name. 但是以上解决了您通过名称访问项目的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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