How to Expand Existing My.Settings StringCollection Array

97 Views Asked by At

I'm a bit rusty and am updating a VB.Net program I wrote in 2013 using VS2017. I needed to expand a My.Settings StringCollection array by one entry. It was User scope, and I could not figure out how to enlarge the existing array, despite hours of online research and testing.

I tried using the .Add method, but was told "not a member of String." I tried deleting "user.config."

Finally I tried this, which worked: 1) At design time, in Settings, I added the extra placeholder value to my StringCollection variable. 2) Change the scope from User to Application. 3) Run the program. 4) Change the scope back to User. I confirmed this worked by inspecting My.Settings at runtime. What I want to know is: Why did this work, and is there a more elegant way to have done this?

    For index = 0 To 5 'Motor numbers
        'Changing upper index limit from 4 to 5 throws an Exception 
        strCommand = "(0," & index.ToString & ")"
        My.Settings.strMotor_Kp(index) = (Kpid(0, index))
        strCommand = "(1," & index.ToString & ")"
        My.Settings.strMotor_Ki(index) = (Kpid(1, index))
        strCommand = "(2," & index.ToString & ")"
        My.Settings.strMotor_Kd(index) = (Kpid(2, index))
    Next
1

There are 1 best solutions below

5
Christoph On

The StringCollection editor is quite a bit limited, you can append lines easily but insert one in the middle is not possible to my knowledge.

But you can also change the values in app.config, recompile the project and then open the settings, then VS is going to asks you

Value of setting 'Foo' was changed in the app.config file. ... Do you want to update the value in the .settings file?

The whole thing should work without having to switch the setting to application- and back to user mode. Just compile it and then the changes are reflected in the app.config (the user.config is generated at runtime when method Save() is called).

Only if the project namespace has changed, then the old entries must be deleted from app.config prior to recompiling the project, or the namespace must be adjusted in the <[...].Properties.Settings> tag).