Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,646 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,126 people online right now. Registration is fast and FREE... Join Now!




Saving your Objects

 
Reply to this topicStart new topic

> Saving your Objects, How to save and load objects.

AdamSpeight2008
Group Icon



post 19 Aug, 2008 - 09:41 PM
Post #1


To enable you save and load your object you need to add <Serializable()> to the start the class you want to save.
Example Class
CODE

<Serializable()> Public Class Contact

    Private mForename As String = ""
    Public Property Forename() As String
        Get
            Return mForename
        End Get
        Set(ByVal value As String)
            mForename = value
        End Set
    End Property
    Private mSurname As String = ""
    Public Property Surname() As String
        Get
            Return mSurname
        End Get
        Set(ByVal value As String)
            mSurname = value
        End Set
    End Property
    Private mState As String = ""
    Public Property State() As String
        Get
            Return mState
        End Get
        Set(ByVal value As String)
            mState = value
        End Set
    End Property
    Private mContactDate As DateTime = #1/1/2000#
    Public Property ContactDate() As DateTime
        Get
            Return mContactDate
        End Get
        Set(ByVal value As DateTime)
            mContactDate = value
        End Set
    End Property

    Private mAddress As String = ""
    Public Property Address() As String
        Get
            Return mAddress
        End Get
        Set(ByVal value As String)
            mAddress = value
        End Set
    End Property
    Private mCity As String = ""
    Public Property City() As String
        Get
            Return mCity
        End Get
        Set(ByVal value As String)
            mCity = value
        End Set
    End Property
    Private mZipCode As String = ""
    Public Property ZipCode() As String
        Get
            Return mZipCode
        End Get
        Set(ByVal value As String)
            mZipCode = value
        End Set
    End Property
    Private mComments As String = ""
    Public Property Comments() As String
        Get
            Return mComments
        End Get
        Set(ByVal value As String)
            mComments = value
        End Set
    End Property
End Class

This the defunct way of save objects.
CODE

Microsoft.VisualBasic.FileSystem.FilePutObject(Of FileNumber, Object)


This is the .net way
CODE

Public Roladex As New List(Of Contact)

To Save
CODE

Dim F As Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim s As IO.Stream
F = New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
s = New IO.FileStream("FileName.bin", IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.None)
F.Serialize(s, Roladex)
s.Close()

To Load
CODE

Dim f As Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim s As IO.Stream
f = New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
s = New IO.FileStream("FileName.bin", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
Roladex = DirectCast(f.Deserialize(s), Object)
s.Close()


Now you save objects serialization-ninja style ph34r.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/23/08 04:48AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month