Search code examples
vb.netarraylistmy.settings

How would you access a specific index in a My.Settings ArrayList using String Format


i've an ArrayList Saved in My.Settings i am trying to Access it using some string, Here is the code so you get what i am trying to do

If listBoxEdit1.SelectedIndex > -1 Then
        My.Settings("Sup" & listBoxEdit1.SelectedIndex + 1 & "(1)") = Convert.ToDouble(Margin2TextBox.Text)
    End If

i need to specify the value for My.Settings.Sup1(1)

How would you do it?

i've Multiple ArrayLists which goes form My.Settings.Sup1 to My.Settings.Sup20 so the listBoxEdit1.SelectedIndex+1 specifies the number of the ArrayList but i can't figure out how to get the index

also tried

My.Settings("Sup" & listBoxEdit1.SelectedIndex + 1 &"("& 1 & ")")

also

My.Settings("Sup" & listBoxEdit1.SelectedIndex + 1).Item(1)

Solution

  • Select StringCollection as Type of your setting.

    enter image description here

    Then you can easily access a specific element with an index.

    Dim setting As StringCollection = My.Settings.MySetting
    Dim value1 = My.Settings.MySetting(1)
    Dim value2 = My.Settings.MySetting(2)
    Dim valueX = My.Settings.MySetting.Item(X)
    

    Edit

    If your setting is called Sup1, you can access it via My.Settings("Sup1") or My.Settings("Sup" & whatever). Then, you have your ArrayList or StringCollection and can access its items like every other ArrayList or StringCollection.

    So your code should probably read:

     My.Settings("Sup" & listBoxEdit1.SelectedIndex + 1)(1) = Convert.ToDouble(Margin2TextBox.Text)
    

    Here's another example:

    My.Settings.MySetting1 = New StringCollection()
    
    My.Settings("MySetting" & 1).Add("FooBar")
    My.Settings("MySetting" & 1).Add("123456")
    
    My.Settings("MySetting" & 1)(0) = "Hey, what's up?"
    
    MsgBox(My.Settings("MySetting" & 1)(1))