String Collections in Settings aren't changing their size

79 Views Asked by At

I have several string collections in my C# project that are stored in Settings. At the beginning of this project, I had initialized these collections to contain 20 values, all being 0s. Lately I've realised I actually need about 100 0s in each collection so I went and added 80 more to each collection. This working fine but now I'm seeing strange things.

I was getting an out of bounded range exception and when I followed it back it came to trying to access an item at the index of 20 in one of my string collections. Hmm. That shouldn't be the case so I go to check my stringCollection that is apparently too small to handle an index of 20 and sure enough still all 100 of my 0s are still there. So then at the breakpoint I hover over my string collection and it says .count = 20. WHAT? I can literally see 100 0s here in the settings.settings file. I counted okay?

So then I went and deleted that string collection, made a new one with the SAME name and SAME contents and it's fine. But I don't want to have to go and redo this for EVERY string collection I have in my settings.

Has anyone come across this? Is it a bug? Why is it that if I delete the collection and then remake it, it suddenly works?

EDIT: My config file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="OPCServer_Simulator.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup><userSettings>
        <OPCServer_Simulator.Properties.Settings>
            <setting name="portName" serializeAs="String">
                <value>COM1</value>
            </setting>
            <setting name="numItems" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="recipeCode" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                    </ArrayOfString>
                </value>
            </setting>
            <setting name="nomThick" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                    </ArrayOfString>
                </value>
            </setting>
            <setting name="nomWidth" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                    </ArrayOfString>
                </value>
            </setting>
            <setting name="meas9" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                        <string>0</string>
                    </ArrayOfString>
                </value>
            </setting> //you get the idea
        </OPCServer_Simulator.Properties.Settings>
    </userSettings>
</configuration>

EDIT 2: My for loop causing he problems

for (int k = 0; k <= 99; k++)
            {
                for (int i = 1; i <= 20; i++) //adding the specified number of items to OPC server
                {
                    var value = Properties.Settings.Default["meas" + i] as System.Collections.Specialized.StringCollection;
                    value[k] = "0";
                }
                var dataSet = Properties.Settings.Default["dateTime"] as System.Collections.Specialized.StringCollection;
                dataSet[k] = " ";
                dataSet = Properties.Settings.Default["recipeCode"] as System.Collections.Specialized.StringCollection;
                dataSet[k] = "0";
                dataSet = Properties.Settings.Default["nomWidth"] as System.Collections.Specialized.StringCollection;
                dataSet[k] = "0";
                dataSet = Properties.Settings.Default["nomThick"] as System.Collections.Specialized.StringCollection;
                dataSet[k] = "0";
            }
0

There are 0 best solutions below