Arraylist of custom classes inside My.Settings

前端 未结 2 1237
一个人的身影
一个人的身影 2021-01-17 01:38

I have a Visual Basic .Net 2.0 program. I\'m moving the settings from an older settings file, to an app.config program settings file. I\'m trying to do this as nicely as p

2条回答
  •  一整个雨季
    2021-01-17 01:59

    MANY NOVICE PROGRAMMERS, EVEN ADVANCED ONES DOESNT KNOW HOW TO SAVE A CUSTOM CLASS IN MYSETTINGS.

    HERE ITS THE PERFECT SOLUTION

    1. YOU CREATE THE CLASS AND SHIT YOU WANNA STORE
    2. CREATE A SETTING TYPE: ARRAYLIST IN MYSETTINGS
    3. IN CODE WHEN YOU WANT TO SAVE, YOU NEED TO GET MEMORYSTREAM OF THE CLASS, SERIALIZE, USE A FORMATTER.

    EXAMPLE

    Dim formatter As Runtime.Serialization.IFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
                Dim ms As New IO.MemoryStream
                formatter.Serialize(ms, original)
                Return ms
    
    1. SAVE TO MYSETTINGS IN ARRAYLIST, ADD VALUE OF TYPE BYTE() LIKE : ms.ToArray

    THIS WILL WORK, AND WHEN THE APP. ITS SHUTDOWN AND OPENED AGAIN, THE ARRAY() WILL STILL BE IN MYSETTINGS

    1. NOW WHEN YOU WANT TO USE THE VALUE YOU NEED TO DESERIALIZE THE BYTES USING A FORMATTER.

    HERE ITS THE CLASS I MADED, THIS HELPS TO SERIALIZE, GET MEMORYSTREAM FROM ANY OBJECT YOU WANT, AND DESERIALIZE.

    Public Class SerializableObjectCopier(Of ObjectType)
            Public Function GetMemoryStream(ByVal original As ObjectType) As IO.MemoryStream
                Dim formatter As Runtime.Serialization.IFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
                Dim ms As New IO.MemoryStream
                formatter.Serialize(ms, original)
                Return ms
            End Function
            Public Function GetCopy(ByVal original As ObjectType) As ObjectType
                Dim formatter As Runtime.Serialization.IFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
                Dim ms As New IO.MemoryStream
                formatter.Serialize(ms, original)
    
                ms.Seek(0, IO.SeekOrigin.Begin)
                Return CType(formatter.Deserialize(ms), ObjectType)
            End Function
            Public Function GetCopy(ByVal ms As System.IO.MemoryStream) As ObjectType
                Dim formatter As Runtime.Serialization.IFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
                ms.Seek(0, IO.SeekOrigin.Begin)
                Return CType(formatter.Deserialize(ms), ObjectType)
            End Function
        End Class
    

    IF YOU NEED SOME HELP OR HAVE QUESTION HERE ITS MY EMAIL:

    bboyse aaron GmA IL doot com

提交回复
热议问题