Deserializing a class

K

kbodily

If I have a class I can serialize it using the following code....

visual basic code:
With myClas
.V1 = Me.TextBox1.Text
.V2 = Me.TextBox2.Text
.V3 = Me.TextBox3.Text
.V4 = Me.TextBox4.Text
End With

Dim fs As New IO.FileStream(BINfName,
IO.FileMode.OpenOrCreate)
Dim bf As New BinaryFormatter
bf.Serialize(fs, myClas)
fs.Close()



and I can Deserialize using this code....

visual basic code:
Dim fs As New IO.FileStream(BINfName, IO.FileMode.Open)
Dim bf As New BinaryFormatter
Dim newClass As mC = CType(bf.Deserialize(fs), mC)
fs.Close()


This is all fine and good, but if at some time after I serialize the
class, I add a variable to the class I get an error when I deserialize
it (it makes sense that it should error). Is there a way to avoid the
error and just allow the new class variable to not be assigned a value
or to retain the default value assigned when the class is instanced?
thanks
kevin
 
T

Tom Shelton

If I have a class I can serialize it using the following code....

visual basic code:
With myClas
.V1 = Me.TextBox1.Text
.V2 = Me.TextBox2.Text
.V3 = Me.TextBox3.Text
.V4 = Me.TextBox4.Text
End With

Dim fs As New IO.FileStream(BINfName,
IO.FileMode.OpenOrCreate)
Dim bf As New BinaryFormatter
bf.Serialize(fs, myClas)
fs.Close()



and I can Deserialize using this code....

visual basic code:
Dim fs As New IO.FileStream(BINfName, IO.FileMode.Open)
Dim bf As New BinaryFormatter
Dim newClass As mC = CType(bf.Deserialize(fs), mC)
fs.Close()


This is all fine and good, but if at some time after I serialize the
class, I add a variable to the class I get an error when I deserialize
it (it makes sense that it should error). Is there a way to avoid the
error and just allow the new class variable to not be assigned a value
or to retain the default value assigned when the class is instanced?
thanks
kevin

If you change the class you need to manually implement the
ISerializable interface to handle the new variables. Look in the docs
for examples. You can post for more specific help, if needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top