|
-
Jun 1st, 2004, 06:12 AM
#1
What is Serialization? [Resolved]
What is serialization and why/when is it used?
How important is it in VB.NET?
Last edited by mendhak; Jun 1st, 2004 at 11:02 PM.
-
Jun 1st, 2004, 06:21 AM
#2
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.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 1st, 2004, 07:08 AM
#3
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.
?
-
Jun 1st, 2004, 07:35 AM
#4
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?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 1st, 2004, 08:04 AM
#5
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?
I was just giving an example situation. I'm no expert. 
What would be the ideal way to go about it from your paragraph above?
-
Jun 1st, 2004, 08:18 AM
#6
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.
Last edited by wossname; Jun 1st, 2004 at 08:23 AM.
I don't live here any more.
-
Jun 1st, 2004, 08:25 AM
#7
Yes, I'd love to see code fo this!
You shall get DW Soon!!!
-
Jun 1st, 2004, 08:38 AM
#8
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!
I don't live here any more.
-
Jun 1st, 2004, 11:02 PM
#9
Thanks a lot!
Much appreciated.
-
Jun 2nd, 2004, 02:42 AM
#10
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...
VB Code:
dim x as MyClass = MyClass.FromFile("Path")
...to call it.
Last edited by wossname; Jun 2nd, 2004 at 07:49 AM.
I don't live here any more.
-
Jun 2nd, 2004, 03:35 AM
#11
Hmm... I knew I was missing something. 
I swear, if I didn't have a third eye, I wouldn't know about my second.
-
Jun 2nd, 2004, 06:49 AM
#12
Member
So the object that you're loading data into in this case is BoundaryDataStorage?
-
Jun 2nd, 2004, 07:48 AM
#13
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.
I don't live here any more.
-
Jun 15th, 2004, 10:06 AM
#14
Hyperactive Member
if any one is wondering you need to import
Imports System.io
Imports System.Runtime.Serialization.Formatters.Binary
for woss's code
-
Jun 15th, 2004, 11:14 AM
#15
Hyperactive Member
and put <Serializable()> by any classes you will be Serializaing
-
Jun 15th, 2004, 12:42 PM
#16
Hey its a learning process, I didn't want to give them ALL the answers!
I don't live here any more.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|