Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Project Settings don't seem to do well with serializing and saving values beyond the basic types. What you can do is use a User-scoped string-valued setting to store a serialized Dictionary.</p> <p>For my example, I created a setting named <code>SerializedKeyPercentDictionary</code> of Type <code>string</code> and Scope <code>User</code>. I am using JSON for my serialization, as it creates a string of smaller length than most other serializations. For this, you will need to add a reference to System.Runtime.Serializations. With this setting and that reference in place, you can then make a global helper class to provide a strongly-typed dictionary for managing your key-to-percent mapping:</p> <pre><code>Public Class KeyPercentHelper Private Shared _keyPercentDictionary As Dictionary(Of Integer, Decimal) Private Shared _initLock As Object = New Object() Public Shared ReadOnly Property KeyPercentDictionary As Dictionary(Of Integer, Decimal) Get If (_keyPercentDictionary Is Nothing) Then InitializeDictionary() End If Return _keyPercentDictionary End Get End Property Shared Sub New() AddHandler My.Settings.SettingsLoaded, AddressOf HandleSettingsLoad AddHandler My.Settings.SettingsSaving, AddressOf HandleSettingsSaving End Sub Private Shared Sub InitializeDictionary() ' Load dictionary from User Setting. SyncLock _initLock If (_keyPercentDictionary Is Nothing) Then If (String.IsNullOrEmpty(My.Settings.SerializedKeyPercentDictionary)) Then _keyPercentDictionary = New Dictionary(Of Integer, Decimal)() Else Dim ser As New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(Dictionary(Of Integer, Decimal))) Using memStream As New System.IO.MemoryStream() Using writer As New System.IO.StreamWriter(memStream) writer.Write(My.Settings.SerializedKeyPercentDictionary) writer.Flush() memStream.Position = 0 _keyPercentDictionary = CType(ser.ReadObject(memStream), Dictionary(Of Integer, Decimal)) End Using End Using End If End If End SyncLock End Sub Private Shared Sub HandleSettingsLoad(ByVal sender As Object, ByVal e As EventArgs) If (_keyPercentDictionary Is Nothing) Then InitializeDictionary() End If End Sub Private Shared Sub HandleSettingsSaving(ByVal sender As Object, ByVal e As EventArgs) ' Ensure User Setting value is updated before save. Dim ser As New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(Dictionary(Of Integer, Decimal))) Using memStream As New System.IO.MemoryStream() ser.WriteObject(memStream, _keyPercentDictionary) memStream.Position = 0 Using reader As New System.IO.StreamReader(memStream) My.Settings.SerializedKeyPercentDictionary = reader.ReadToEnd() End Using End Using End Sub End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload