-
Sep 29th, 2017, 11:23 AM
#1
Thread Starter
Junior Member
[RESOLVED] My.Settings using custom class
Hello everyone, i've done quite a bit of researching online to try and fix the problem i'm having with no luck. I have a custom collection class that i have saved in my.settings under project->settings.
the custom class inherits System.Collections.CollectionBase
Of course the setting is not initialized in the IDE under settings. I've tried to initialize it in code but it always is = nothing when i restart my program.
This is the code i'm using in the Load event to try and initialize my setting. Any ideas why i cant get my.setting to save?
my.setting type is MP3ServerCollection (custom class)
this is the MP3ServerCollection class code:
Code:
Public Class MP3ServerCollection
Inherits System.Collections.CollectionBase
Public Sub Add(ByVal amp3server As MP3Server)
List.Add(amp3server)
End Sub
Public Sub Remove(ByVal index As Integer)
If index > Count - 1 Or index < 0 Then
Console.WriteLine("Index Not Valid")
Else
List.RemoveAt(index)
End If
End Sub
Public ReadOnly Property Item(ByVal index As Integer) As MP3Server
Get
Return CType(List.Item(index), MP3Server)
End Get
End Property
End Class
And here it the code for the MP3Server class
Code:
Imports System.Runtime.Serialization
Imports System.Collections
Imports System.Collections.Generic
<Serializable()> Public Class MP3Server
Public Sub New()
_nickname = ""
_filelist = ""
_filelistdate = ""
End Sub
Private _nickname As String
Public Property NickName As String
Get
Return _nickname
End Get
Set(value As String)
_nickname = value
End Set
End Property
Private _filelist As String
Public Property FileList As String
Get
Return _filelist
End Get
Set(value As String)
_filelist = value
End Set
End Property
Private _filelistdate As String
Public Property FileListDate As String
Get
Return _filelistdate
End Get
Set(value As String)
_filelistdate = value
End Set
End Property
End Class
Here is the code i use to try and initialize the setting.
Code:
If My.Settings.servers Is Nothing Then
Dim servercollection As MP3ServerCollection = New MP3ServerCollection()
Dim server As New MP3Server
servercollection.Add(server)
My.Settings.servers = servercollection
My.Settings.Save()
End If
Any help would be greatly appreciated.
Anyday
-
Sep 29th, 2017, 12:25 PM
#2
Re: My.Settings using custom class
How are you creating the servers item in Settings? I don't know if things have changed, but as recently as 2010 there was no way to save a custom collection to a setting directly, a you are doing. You'd have to serialize it to a Base64 encoded string, and save it as a string setting.
My usual boring signature: Nothing
-
Sep 29th, 2017, 12:35 PM
#3
Re: My.Settings using custom class
Yeah, your best bet when not using EXACTLY the types specified in My.Settings is to serialize your type to a String and save it that way. It doesn't have to be Base64-encoded, it can be any format that is convenient for you to parse.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Sep 29th, 2017, 12:42 PM
#4
Re: My.Settings using custom class
Originally Posted by anyday901
the custom class inherits System.Collections.CollectionBase
That's not been recommended for over a decade now. Since .NET 2.0 and VS 2005, a custom collection should be derived from System.Collections.ObjectModel.Collection(Of T). Did you choose that option for a specific reason or did you just not realise that there is a better option?
-
Sep 29th, 2017, 01:41 PM
#5
Thread Starter
Junior Member
Re: My.Settings using custom class
Originally Posted by jmcilhinney
That's not been recommended for over a decade now. Since .NET 2.0 and VS 2005, a custom collection should be derived from System.Collections.ObjectModel.Collection(Of T). Did you choose that option for a specific reason or did you just not realise that there is a better option?
I was following an MSDN example that said to use that type. I will try now using the type you suggested.
Anyday
-
Sep 29th, 2017, 01:55 PM
#6
Thread Starter
Junior Member
Re: My.Settings using custom class
jmcilhinney: thanks so much for the tip that fixed me up. Changed the inherits and it works like a charm!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|