VB.NET Trying to read a System.Collections.Specialized.StringCollection in My.Settings returns an InvalidOperationException

StackOverflow https://stackoverflow.com/questions/17143277

Question

In my vb.net project i've created the following settings with the built-in settings manager from visual studio:

  • appVisible (Boolean)
  • saveFusedFiles (Boolean)
  • colors (System.Collections.Specialized.StringCollection)
  • separators (System.Collections.Specialized.StringCollection)

While trying to read "colors" or "separators"¨i get an InvalidOperationException, but reading the boolean variables works.

In my Config file i cannot find my System.Collections.Specialized.StringCollection variables...

As far i know, the instances of the settings variables are automatically created, so this shouldn't be the problem.

This is the constructor where i read the settings:

Public Sub New()

    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    My.Settings.Reload()

    'INIT Separators

    If Not ListView_Separators.Items.Count = 0 Then
        ListView_Separators.Items.Clear()
    End If

    If My.Settings.separators.Count = 0 Then
        My.Settings.separators.Add(",")
        GenerateListViewItem(",")
    Else
        For Each seperator As String In My.Settings.separators
            GenerateListViewItem(seperator)
        Next
    End If
    Button_Add.Enabled = False
    Button_Delete.Enabled = False

    'INIT Colors

    If DataGridView_Colors.Rows.Count > 0 Then
        DataGridView_Colors.Rows.Clear()    'Clear DataGridView
    End If
    For Each color As String In My.Settings.colors  'Add all Colors to DataGridView
        'Add to DataGridView
        Dim splitedColor As String() = New String(1) {1, 1}
        splitedColor = color.Split("_")
        Dim contentText As String
        Select Case splitedColor(1)
            Case 0
                contentText = "New component"
            Case 1
                contentText = "Removed component"
            Case 2
                contentText = "Changed Data"
        End Select
        Dim arrDataGridRow As String() = New String(1) {splitedColor(0), contentText}
        DataGridView_Colors.Rows.Add(arrDataGridRow)
        Dim dgwStyle As New DataGridViewCellStyle
        dgwStyle.ForeColor = Drawing.Color.FromName(splitedColor(0))
        DataGridView_Colors.Rows(DataGridView_Colors.Rows.Count - 1).Cells(0).Style = dgwStyle
    Next

    'INIT View

    If My.Settings.appVisible = True Then
        Checkbox_Visable.Checked = True
    Else
        Checkbox_Visable.Checked = False
    End If

End Sub

My.Settings.separators.Count throws the exception.

Does anyone know how to handle this?

Was it helpful?

Solution

I think this answers why you're not seeing the collection in your config file.

https://stackoverflow.com/a/23452534/3585500

But this answer on the same question seems like a better solution.

https://stackoverflow.com/a/24112414/3585500

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top