Results 1 to 3 of 3

Thread: [RESOLVED] Storing Custom Classes in My.Settings

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Resolved [RESOLVED] Storing Custom Classes in My.Settings

    According to this and this it should be possible to create custom classes and store them in My.Settings.

    I tried doing what those articles recommended, but I can't get the most basic custom class to work:

    VB Code:
    1. Imports System.ComponentModel
    2. Imports System.Configuration
    3.  
    4. <Serializable()> _
    5. Public Class CustomSetting
    6.     Inherits ApplicationSettingsBase
    7.  
    8.     Public Sub New()
    9.  
    10.     End Sub
    11.  
    12.     Public Sub New(ByVal mysetting As String)
    13.         _MySetting = mysetting
    14.     End Sub
    15.  
    16.     Private _MySetting As String
    17.     <UserScopedSetting()> _
    18.     <SettingsSerializeAs(SettingsSerializeAs.Xml)> _
    19.     Public Property MySetting As String
    20.         Get
    21.             Return _MySetting
    22.         End Get
    23.         Set(value As String)
    24.             _MySetting = value
    25.         End Set
    26.     End Property
    27. End Class

    When I run the program, I get the following error:

    Code:
    {"Configuration system failed to initialize"}
    I don't know what I'm doing wrong.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Storing Custom Classes in My.Settings

    I wouldn't bother with serializing to XML. Here's the code I use to perform a binary serialization to a Base64 string, which is a data type you can use in Settings:

    Code:
    Private Function SerializeTheme() As String
            If mConfiguration IsNot Nothing AndAlso mConfiguration.ThemeID <> -1 Then
                Try
                    Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
                    Dim mStream As New System.IO.MemoryStream
                    Dim bReader As New System.IO.BinaryReader(mStream)
    
                    bf.Serialize(mStream, mConfiguration)
                    mStream.Position = 0
    
                    Return Convert.ToBase64String(bReader.ReadBytes(CInt(mStream.Length)))
                Catch ex As Exception
                    Windows.Forms.MessageBox.Show("Failed while saving the theme. The default will be used next time. The error message was: " & Environment.NewLine & Environment.NewLine & ex.Message, "Serial Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return String.Empty
                End Try
            Else
                Return String.Empty
            End If
        End Function
    
        Private Shared Function DeSerializeTheme(ByVal themeAsString As String) As ThemeWrapper
            Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
            Dim mStream As New System.IO.MemoryStream
            Dim bWriter As New System.IO.BinaryWriter(mStream)
    
            If themeAsString = String.Empty Then
                Return Nothing
            End If
            Try
                bWriter.Write(Convert.FromBase64String(themeAsString))
                mStream.Position = 0
    
                Return DirectCast(bf.Deserialize(mStream), ThemeWrapper)
            Catch ex As Exception
                Return Nothing
            End Try
    
        End Function
    ThemeWrapper is the custom type. You can use code to turn a custom class into a string that can be stored in the setting, and use the Deserialize method to turn the string back into the class by changing the ThemeWrapper to whatever type you are working with.

    EDIT: I have no idea why the Serialize method wasn't added the first time, but it is there now.
    Last edited by Shaggy Hiker; Aug 9th, 2012 at 06:14 PM.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Re: Storing Custom Classes in My.Settings

    That worked great! Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width