What is serialization and why/when is it used?
How important is it in VB.NET?
Printable View
What is serialization and why/when is it used?
How important is it in VB.NET?
Its the process of taking an object in memory and persisting it in some way.
Its most important (I think) for client/server communications. Instead of decomposing things into byte arrays to send them over a socket like you did in VB6, you simply open the socket, grab a reference to the I/O stream that belongs to the socket, and serialize pretty much any whole object you want to the stream. Then on the other end, you just deserialize the object from the stream.
The SOAP protocol uses XML serialization as a means of transmitting object data.
You can also write a whole object to a file and just grab it back out again.
Pretty cool stuff.
:afrog:
Tell me if this is a good example of the usage of serialization:
I create a class that contains properties such as "news title" and "news description." Everytime I update this object with different, new news items, I serialize it and send it across to some other computer.
That computer takes this (XML file) object and uses it on its website. Kind of like a news feed.
?
Sounds like a decent candidate.
Does the other computer just sit there and listen for update calls, or would you just be copying a file over to it?
I was just giving an example situation. I'm no expert. :)Quote:
Originally posted by crptcblade
Sounds like a decent candidate.
Does the other computer just sit there and listen for update calls, or would you just be copying a file over to it?
What would be the ideal way to go about it from your paragraph above?
I normally use Binary Serialization to accomplish my deeds.
Particularly useful for cloning things not normally cloneable.
I have code if you want it.
Binary Serialization is better than XML because it stores ALL member data not just the public bits. Any bits that you don't want serialized can be ignored with <NonSerialized()> attribute.
I'm currently using BS (unfortunate abbrev I know) to save an entire heirarchical data structure to a single file. Dead fast, safe and efficient. Cool.
Yes, I'd love to see code fo this!
You shall get DW Soon!!!
OK, basically in a nutshell this is it. I have taken this code straight out of my current app. I've removed tha bits specific to my program that would be irrelevant to you...
VB Code:
Public Function Save(ByVal path As String) As Boolean 'serializes this class instance to a file in binary format Dim binser As BinaryFormatter Dim output As Stream Try binser = New BinaryFormatter output = New FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None) binser.Serialize(output, Me) 'where the instance is actually serialized to the stream output.Close() Return True 'success! :) Catch ex As Exception If Not IsNothing(output) Then output.Close() notify the user of the error if you want. Return False 'failure :( End Try End Function
Copy that code into a class and it will then be able to serialize itself to a file!
Thanks a lot!
Much appreciated.
Sorry, I neglected to supply you with means of loading the file back into the object!
VB Code:
Shared Function FromFile(ByVal path As String) As !!YOUR_CLASS_NAME_HERE!! 'this method returns an object loaded from the file specified by path 'if the file path doesn't exist then a new object is created and returned Dim temp As !!YOUR_CLASS_NAME_HERE!! If File.Exists(path) Then Dim binser As BinaryFormatter Dim input As Stream Try 'load the file binser = New BinaryFormatter input = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None) 'open the file temp = CType(binser.Deserialize(input), !!YOUR_CLASS_NAME_HERE!!) 'extract instance from stream input.Close() Catch ex As Exception If Not IsNothing(input) Then input.Close() temp = Nothing Return Nothing End Try Else temp = New !!YOUR_CLASS_NAME_HERE!! temp.Path = path End If Return temp End Function
It is a shared function.
so you would use...
...to call it. :)VB Code:
dim x as MyClass = MyClass.FromFile("Path")
Hmm... I knew I was missing something. :)
I swear, if I didn't have a third eye, I wouldn't know about my second.
So the object that you're loading data into in this case is BoundaryDataStorage?
Oops, my mistake... Good point Yak.
Mendhak: Check out the top line of the FromFile() code again. I have edited it cos it was wrong. But you probably guessed that anyway.
EDIT.
if any one is wondering you need to import
Imports System.io
Imports System.Runtime.Serialization.Formatters.Binary
for woss's code
and put <Serializable()> by any classes you will be Serializaing
Hey its a learning process, I didn't want to give them ALL the answers! ;) :D