Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET File I/O and Registry > System.IO for saving a collection


Reply
 
Thread Tools Display Modes
  #1  
Old 02-05-2008, 11:09 AM
XL Jedi XL Jedi is offline
Freshman
 
Join Date: Dec 2007
Location: Palm Beach, Florida
Posts: 26
Default System.IO for saving a collection


I'd like to create a routine that would allow me to save/load a collection to a file. I'd like it to (preferrably) not just be a text file. Is there any simple binary file IO code around?

Any suggestions would be appreciated.
Reply With Quote
  #2  
Old 02-05-2008, 12:16 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

It depends on what you mean by "a collection". If it's a built-in .NET collection, check the documentation and see if it is marked with SerializableAttribute. If not, see if it implements ISerializable. If so, then binary serialization will work without any additional work. Most collection classes in the .NET Framework are marked with the attribute, but I have not verified if all of them are.

If it's a collection you created yourself, you'll have to either mark your class with SerializableAttribute or implement ISerializable (don't forget the special constructor!).

Once your collection class is serializable, look at this thread for a bare-bones example of serialization.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 02-05-2008, 12:33 PM
XL Jedi XL Jedi is offline
Freshman
 
Join Date: Dec 2007
Location: Palm Beach, Florida
Posts: 26
Default

It's my own collection...

I have a custom class of waypoints (stored in a collection) that I need to save and then load to allow users to store and retrieve plots they've created. If it were just a matter of saving coordinates it would be simple, but the waypoints have a lot of extra properties that I don't want to have to write individually.
Reply With Quote
  #4  
Old 02-07-2008, 10:18 AM
XL Jedi XL Jedi is offline
Freshman
 
Join Date: Dec 2007
Location: Palm Beach, Florida
Posts: 26
Default

For those who might be interested, saving a collection (or list) of a custom class turned out to be painfully easy!

Thanks to AtmaWeapon for a push in the right direction (by giving me the "Serializable" keyword to research.)

In my case I have a plotting application that saves graphic objects as custom "Unit" classes. The instances of the unit class are stored in a collection (although a list could also be used) called "units".

A simplified example of my custom class now looks like this:
Code:
<Serializable()> _
Public Class Cls_Unit

    <NonSerialized()> _
    Public MyPath As GraphicsPath
    Public Name As String = Nothing
    Public Type As String = Nothing
    Public ObjtName As String = Nothing
    Public Selected As Boolean = False

End Class
Notice above I had to configure my class for serialization by adding the Serializable attribute. Also worth noting, if you have a property in your custom class that is typed as another custom class, that child class also has to be marked as Serializable. If you fail to configure any related objects as Serializable, you'll get a SerializationException error at runtime. And it will point out to you specifically which objects are not properly marked.

In my case above I was getting an error on the GraphicsPath object. Since I generate that path with a draw method anyway, it wasn't necessary to save it, so I just marked that object "NonSerialized" and the BinaryFormatter now has no issues with it.

Now in my app, as I plot a new unit, the unit is stored in a collection called "units". The collection of units is what defines the resulting graphic plot.

To save the plot as a binary file the code is really very simple:
Code:
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO

Sub Write_BinFile()
        Dim binFormat As BinaryFormatter = New BinaryFormatter()
        Dim fStream As Stream = New FileStream("SaveData.dat", FileMode.Create, FileAccess.Write, FileShare.None)
        binFormat.Serialize(fStream, Units)
        fStream.Close()
    End Sub
Then to re-load my Units collection from the saved bin file:
Code:
Sub Read_BinFile()
        Dim fstream As Stream = File.OpenRead("SaveData.dat")
        Dim binFormat As BinaryFormatter = New BinaryFormatter()
        Units = binFormat.Deserialize(fstream)
        fstream.Close()
    End Sub
I have to admit, I was actually a little dumbfounded to learn how simple a task this turned out to be!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->