VB.net save a list to settings

Murray Brown
Murray Brown used Ask the Experts™
on
Hi. I was given the following code that I use to save a list of items
against a class object. I want to be able to assign this to a Setting
How do I do this so that when I close and open the project I can
reuse the list?

Module Module1

    Sub Main()
        Dim objCls As New Class1

        objCls.JointTables = "Tab1"
        objCls.JointTables = "Tab2"
        objCls.JointTables = "Tab3"
        objCls.JointTables = "Tab2"


    End Sub

End Module

Public Class Class1


    Private _pJoinTables As String

    Public Sub New()
        _pJoinTables = ""
    End Sub

    Property JointTables() As String
        Get
            Return _pJoinTables
        End Get
        Set(ByVal sValue As String)
            If (Not _pJoinTables.Contains(sValue)) Then
                _pJoinTables = _pJoinTables & "," & sValue 'Error on this line first &
            End If
        End Set
    End Property

End Class
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
You could use XML serialization for this purpose. For example, to dump the object out as XML to a string setting you could do:

Dim strWriter As New System.IO.StringWriter()
Dim serializer As New System.Xml.Serialization.XmlSerializer(GetType(Class1))

serializer.Serialize(strWriter, objCls)
My.Settings.TheList = strWriter.ToString()
My.Settings.Save()

Open in new window


...and to restore it from the setting you could do:

Dim strReader As New System.IO.StringReader(My.Settings.TheList)
Dim deserializer As New System.Xml.Serialization.XmlSerializer(GetType(Class1))
Dim restored As Class1 = DirectCast(deserializer.Deserialize(strReader), Class1)

Open in new window


Since the data is XML when it is serialized, you could open the file outside of code to make changes--if you wanted to.
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial