Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27367

VS 2010 BinaryFormatter: Default values?

$
0
0
Hello everybody,

I have a little problem.

I am using a BinaryFormatter to serialize and deserialize objects to/from a file.
Some of the classes which are being saved and loaded might change over time.
I would like the following behavior during deserializing:
- If the serialized object contains a superfluous member, it is discarded (this works correctly).
- If the class of the object has members defined which are not found in the serialized data, the default values specified in the class (e.g. FooBar As String = "abc") should be used (this doesn't work).

Currently, if I add a new member to the class and then load data from an older file (which doesn't contain this member), the member becomes some kind of null - that is: For Integer/Double/... it becomes 0, for Boolean it becomes False and for String and some kind of Object it becomes Nothing.

I would like a simple way to change this behavior without having to write a special "version updater" code (I found examples for that in MSDN), because I want to be able to add a member to the class without having to update some other code!

Is that possible?

Best regards,
Cherry

Viewing all articles
Browse latest Browse all 27367

Trending Articles