Page 1 of 1

Saving your Objects How to save and load objects. Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Post icon  Posted 19 August 2008 - 10:41 PM

To enable you save and load your object you need to add <Serializable()> to the start the class you want to save.
Example Class
<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.
Microsoft.VisualBasic.FileSystem.FilePutObject(Of FileNumber, Object)



This is the .net way
Public Roladex As New List(Of Contact)


To Save
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
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:

Is This A Good Question/Topic? 1
  • +

Page 1 of 1