English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

as much information as you can provide would be very much appreciated. also, my ultimate goal is to be able to save an object. ...a very simple object. ...what kind of...Writer do i need to do that?

2007-08-07 16:47:22 · 3 answers · asked by the_honorable_spm 2 in Computers & Internet Programming & Design

3 answers

The key top serializing an entire class model is the BinaryFormatter class, found in the RunTimeSerialization.Formatters.Binary namespace.

[Note: The entire name of the namespace does not display properly in this Answers editor. Answers editor truncates the name—due to the series of dots used. You must look up the full name of the namespace, and make the appropriate changes.]

Basically, the class's Serialize() method stores each member variable of the specified object in turn a a binary image of its data in memory. When a member variable is in fact a non-value member, it stores an image of each member variable of that owned object—and so on, until the entire object model has been persisted. For it to do this, every class in the object model must be marked with the attribute.

The result is a binary file of data that can be used to reconstruct the object model.
_____________________

Let's say that you are closing a form (frmMyForm) that has used a instance, InstanceOf_myClass (which is an instance of a class called myClass).

To serialize the class instance called InstanceOf_myClass:

Private Sub frmMyForm_Closing()
First mark the class as serializable with the class attribute marker .
myClass
'Class members
'Class properties
'Class methods
End Class

Dim fileName As String = "c:/Data/Bin/YourClass.dat"
Dim str As New IO.FileStream(fileName, IO.FileMode.Create)

Dim b as New runtime.Serialization.Formatters. binary.BinaryFormatter()

b.Serialize(str.InstanceOf_myClass)
End Sub 'Sub frmMyrForm_Closing
_____________________

To de-serialize the object when your re-load frmYourForm

Private Sub frmMyForm_Load()
Dim fileName As String = "c:/Data/Bin/YourClass.dat"

'Declare an instance of your class
Dim NewInstanceOf_myClass As myClass


If IO.File.Exists(fileName) Then
Dim str As New IO.FileStream(fileName, IO.FileMode.Open)

Dim b As New Runtime.Serialization.Formaters.Binary.BinaryFormatter()

'Cast the resulting objet to the correct type to complete the de-serialization process
NewInstanceOf_myClass = CType(b, Deserialize(str), InstanceOf_myClass)

str.Close
End If
End Sub

2007-08-07 18:25:17 · answer #1 · answered by Einstein 5 · 0 0

You mean you want to implement Serializable and got the object persistence to disk? Do your research on those topics but I think StreamWriter is the one you need anyway.

2007-08-07 23:58:47 · answer #2 · answered by Andy T 7 · 0 0

difference between binarywriter and streamwriter using streamreader readline writing byte data using binary writer using filestream.....

for more help contact to exert on this field i am providing url so concern id of this just go through and get the best answers...

http;//www.commediait.com

2007-08-08 01:08:55 · answer #3 · answered by top s 3 · 0 0

fedest.com, questions and answers