473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Deserializing a class

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

Jan 19 '07 #1
1 847

kb*****@charter.net wrote:
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.

--
Tom Shelton

Jan 20 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas | last post by:
Hi, I implemented a composite pattern which should be serializable to xml. After spending some time in the newsgroups, i finally managed serializing, even with utf-8 instead of utf-16, which...
4
by: Wayne Wengert | last post by:
Using VB.NET I want to read in an XML file that has an array of objects and then step through the resulting array in code. I build a class to define the structure and I am running code to read in...
0
by: Amit | last post by:
Hi Friends, I am stuck in a situation that i need help for.The situation is like i have an xml coming from the database in the format <BASE> <Base> <UserName>A</UserName>...
0
by: Casey | last post by:
So I'm using XmlSerializer to serialize out a wrapper object that contains an arbitrary number of other objects. The class definitions listed below are made to be very generic. Some of the...
2
by: Andrew G. J. Fung | last post by:
Can anyone please explain to me why the code below throws an exception? It seems to occur when deserializing an instance of a subclass, whose base class holds a struct, where said struct holds an...
2
by: Earl Teigrob | last post by:
I am saving and restoring value types such as Int32, DateTime and Boolean in strings. I was wondering if there is a mechanism build into .NET for serializing and deserializing these to string...
1
by: Bob Rock | last post by:
Hello, always having to validate an XML stream against a XSD may add up an important overhead. My XMLs are usually the result of serializing a class instance and often in my applications what I...
2
by: Phillip Galey | last post by:
I have an object called Place which contains only string properties and has the <Serializable()> flag before the class name declaration. I also have a collection object called Places, which is...
1
by: =?Utf-8?B?SmVyZW15X0I=?= | last post by:
I am working on an order entry program and have a question related to deserializing nodes with nested elements. The purchase order contains multiple line items which I select using an...
0
by: ullner | last post by:
I have an XML fil that looks like this: <Environment> <AreaOfInterest> <Name>ScenarioMap</Name> <UpperRight> <GDC> <Latitude>-179</Latitude> ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.