In ASP.NET I've found you cannot store a class in a session or viewstate
variable without first marking it as Serializable.
Example:
<Serializable()>_
Public Class Foo
End Class
In a Windows Form if you mark a class Serializable you can save/load it to
disk with code like this:
' class to persist to disk...
<Serializable()> _
Public Class UserSettings
Private _username As String
Public Property UserName() As String
Get
Return _username.ToLower
End Get
Set(ByVal Value As String)
_username = Value.ToLower
End Set
End Property
End Class
' helper class to save/load form disk...
Imports System
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Public Class UserSettingsDB
Private Sub New()
End Sub
Public Shared Function LoadSettings() As UserSettings
Dim mySettings As New UserSettings
Dim s As String = "c:\myapp.bin"
If File.Exists(s) Then
Dim streamOut As FileStream
Try
Dim formatter As New BinaryFormatter
streamOut = New FileStream(s, FileMode.Open,
FileAccess.Read)
mySettings = CType(formatter.Deserialize(streamOut),
UserSettings)
Catch
Finally
streamOut.Close()
End Try
End If
Return mySettings
End Function
Public Shared Sub SaveSettings(ByVal mySettings As UserSettings)
Dim s As String = "c:\myapp.bin"
Dim formatter As New BinaryFormatter
Dim streamIn As New FileStream(s, FileMode.Create,
FileAccess.Write)
formatter.Serialize(streamIn, mySettings)
streamIn.Close()
End Sub
End Class
Greg