*Gurus* Derek Stone Posted December 3, 2002 *Gurus* Posted December 3, 2002 Say you have a class named myDog. You take the class myDog, along with all its properties, such as size and color, and persist it in a format that can be saved to a file or sent over a network. It's basically like taking a live dog, and saving it to a file. Of course you can also deserialize what's been serialized, essentially taking the file and turning it back into the class myDog. If you're further interested in the topic, you'll discover both binary and XML serialization. Binary serialization can save an object to memory, save it to disk or send it over a network. XML serialization saves the object in a format that is human readable, such as the following: <dog> <color>black</color> <size>4</size> </dog> It's a fairly useful technology. Learn more about it in MSDN: http://ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpovrserializingobjects.htm Quote Posting Guidelines
*Gurus* divil Posted December 3, 2002 *Gurus* Posted December 3, 2002 I find it useful for file formats. Where before you might invent a file format for your program, read it line by line and parse it to create your class, now you just use a couple of lines to serialize an instance of the class to disk in XML format (or other). Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Bucky Posted December 3, 2002 *Experts* Posted December 3, 2002 Derek, I didn't know dogs had sizes... or is that supposed to be its shoe size? :D Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
*Gurus* Derek Stone Posted December 3, 2002 *Gurus* Posted December 3, 2002 Here's a size chart: 4 = Large 3 = Medium 2 = Small 1 = Bucky Quote Posting Guidelines
*Experts* Bucky Posted December 3, 2002 *Experts* Posted December 3, 2002 So it's like golf... the smaller the number, the better. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
*Gurus* Derek Stone Posted December 3, 2002 *Gurus* Posted December 3, 2002 If that's how you need to look at, then yes, it is. ;) Quote Posting Guidelines
Moderators Robby Posted December 3, 2002 Moderators Posted December 3, 2002 yeaaah that's it, golf, like golf scores Bucky. hehehe Quote Visit...Bassic Software
Leaders quwiltw Posted December 3, 2002 Leaders Posted December 3, 2002 Speaking of serialization, I'm having trouble serializing collections. I've got a custom object that inherits an arraylist which is filled with custom objects, I can serialize the single object fine but the custom collection gives me the following exception. Any ideas? An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll Additional information: There was an error generating the XML document. Quote --tim
Leaders quwiltw Posted December 3, 2002 Leaders Posted December 3, 2002 When I choose to Break, the locals screen highlights {Length=4} in red, if that helps... Quote --tim
*Gurus* divil Posted December 3, 2002 *Gurus* Posted December 3, 2002 Collections should serialize ok, dictionaries won't though. Read the docs on the xml serializers or search google, I found many resources on serializing collections when I had to do it. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Leaders quwiltw Posted December 3, 2002 Leaders Posted December 3, 2002 Yep, I think I'm doing what I've read, and dumbed it down to be a fairly simple test. It has problems with any of my custom objects being in the arraylist. Public Class Form1 Inherits System.Windows.Forms.Form Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim cfl As New ArrayList() cfl.Add(New SimpleDate()) cfl.TrimToSize() Dim ser As XmlSerializer = New XmlSerializer(GetType(ArrayList)) Dim writer As TextWriter = New StreamWriter("adminSettings.xml") ser.Serialize(writer, cfl) writer.Close() End Sub End Class <Serializable()>Public Class SimpleDate Public SimpleDate As DateTime End Class Quote --tim
Leaders quwiltw Posted December 4, 2002 Leaders Posted December 4, 2002 Hmmm... Must be an XML thing. It serializes in binary fine. Quote --tim
*Gurus* divil Posted December 4, 2002 *Gurus* Posted December 4, 2002 Maybe the serializers only serialize public properties, and not fields? It would definitely be worth a try. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
jhilb Posted December 4, 2002 Author Posted December 4, 2002 Thanks for the explanation. I think I understand the concept. What is the purpose of the <serializable> tag I sometimes see on functions and subs? p.s. I couldn't get that link to work Quote
Leaders quwiltw Posted December 4, 2002 Leaders Posted December 4, 2002 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemSerializableAttributeClassTopic.asp Quote --tim
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.