0

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
  • How are you creating New Specialized.StringCollection and saving it? – CruleD Jul 05 '19 at 16:47
  • I never used the New method. The StringCollection (e.g., My.Settings.strMotor_Kp) was originally created in 2013 by making 5 entries via MyProject|Settings. All I wanted to do (and did, using the method described) was to expand it to 6 entries. I added the 6th entry as described. – petercat Jul 06 '19 at 21:21

1 Answers1

1

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).

Christoph
  • 3,322
  • 2
  • 19
  • 28
  • All of my variables, created in 2013 using MyProject|Settings, were designated "user scope," and none as "application scope." As I understand it, this means that none of the entries in app.config were ever used until I toggled the variables of interest to application scope and back. (As an aside, I see no reason I could not use my method to manually "insert" a new value anywhere in the existing StringCollection array, not just append it to the end of the array.) – petercat Jul 06 '19 at 21:30
  • @petercat How about creating a new one, adding current things to it, add new thing, then save it on place of old one? – CruleD Jul 06 '19 at 23:40
  • @petercat: No, they are reflected in the app.config anyway, as the user settings are taken from it if they don't exist. – Christoph Jul 07 '19 at 07:45
  • CruleD: I think your suggestion to create a new one would work. I just want to see if I can avoid the extra effort, and use the legacy data. After all, a legacy StringCollection array could be quite large in some applications, increasing the risk of typos. – petercat Jul 07 '19 at 11:57
  • @Christoph: What you say is true, but in the challenge I'm posing the user settings do exist (I don't want to lose them, or re-enter them, as CruleD suggests). – petercat Jul 07 '19 at 12:06