Search code examples
c#.netfor-loopproperties.settings

Looping through Properties.Settings using a string


Is there a way to loop through Settings using an identifier like the following

for (int i = 1; i < 6; i++)
{
    Properties.Settings.Default.["S" + i.ToString()]= 0;//identifier expected
}

To substitute these:

Properties.Settings.Default.S1 = 0;
Properties.Settings.Default.S2 = 0;
Properties.Settings.Default.S3 = 0;
Properties.Settings.Default.S4 = 0;
Properties.Settings.Default.S5 = 0;

Problem is that it's throwing an identifier expected error


Solution

  • Try it like so, i.e. remove the dot '.' after Default to invoke the indexing operator

    Properties.Settings.Default["S" + i.ToString()]= 0