By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,784 Members | 1,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,784 IT Pros & Developers. It's quick & easy.

Serializable class

P: n/a
Hi

What is a Serializable class compared to a normal class?

Thanks

Regards
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
i think it means you can use the serializer to persist the data
to an xml file

not sure how to do that or even how to get the samples to work

"John" <jo**@nospam.infovis.co.uk> wrote in message
news:OB*************@tk2msftngp13.phx.gbl...
Hi

What is a Serializable class compared to a normal class?

Thanks

Regards

Nov 20 '05 #2

P: n/a
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
"john m" <jm@haengineers.com> wrote in message
news:eK**************@TK2MSFTNGP12.phx.gbl...
i think it means you can use the serializer to persist the data
to an xml file

not sure how to do that or even how to get the samples to work

"John" <jo**@nospam.infovis.co.uk> wrote in message
news:OB*************@tk2msftngp13.phx.gbl...
Hi

What is a Serializable class compared to a normal class?

Thanks

Regards


Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.