Simple Generalized File I/O & Deep Copy Cloning of Any Object

MyForumID

Active member
Joined
Sep 25, 2005
Messages
26
Programming Experience
Beginner
Hello All,

I found a couple of examples on the web that really helped me with regards to how to save, load and deep copy clone (i.e. not just copying the reference but the actual data) any object using streams. I've generalized them and wanted to share them in case others find them useful. The sites I used were:
http://visualbasic.about.com/od/learnvsnet/l/blecvbnet10304.htm
http://www.devx.com/dotnet/Article/6971/0/page/6

VB.NET:
'These libraries must either be included or explicitly called when needed.
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Runtime.Serialization.Formatters.Binary
[/SIZE][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.IO[/SIZE]
 
[SIZE=2][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] CloneObject([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] originalObject) [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][SIZE=2] BinFormatter [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] BinaryFormatter
[/SIZE][SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][SIZE=2] memStream [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] MemoryStream
 
    BinFormatter.Serialize(memStream, originalObject)
    memStream.Position = 0
    CloneObject = BinFormatter.Deserialize(memStream)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] SaveObjectFile([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] fileName [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] saveObject [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][SIZE=2] fileStream [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileStream(fileName, FileMode.Create)
[/SIZE][SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][SIZE=2] fileFormatter [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] BinaryFormatter
 
    fileFormatter.Serialize(fileStream, saveObject)
    fileStream.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] LoadObjectFile([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] fileName [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][SIZE=2] fileStream [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileStream(fileName, FileMode.Open)
[/SIZE][SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][SIZE=2] fileFormatter [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] BinaryFormatter
 
    LoadObjectFile = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](fileFormatter.Deserialize(fileStream), [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2])
    fileStream.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][/SIZE][SIZE=2]
'Notice that the Clone and Load methods are functions. This allows them to be called using the CType function.
'Examples of how to use all three are:
 
MyClonedObject = CType(CloneObject(OriginalObject), MyObjectType)
MyLoadedObject = CType(LoadObjectFile(SavedObjectFilename), MyObjectType)
SaveObjectFile(SavedObjectFilename, ObjectToSave)
 
[/SIZE]


The nice thing is that the only place you actually need to specify the object type is when calling the Clone and Load functions. They can all be used for any and all object types within the same program without changing the method codes at all.


I don't know if the I/O and copying speed is faster or slower using binary streams than using text or member by member copies, but this does make saving/loading/cloning objects very easy.

Hope this helps and good luck :)
 
Back
Top