473,385 Members | 2,180 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,385 software developers and data experts.

New versions vs. serialization

Hi,

I'm writing a program which is, and will continue to be, constantly extended
with functionality that require information. Until now the source of this
information has been found and modified using notepad in a txt file that was
parsed into fields in objects. (This solution because this was the format
that an older fortran program was using and we needed to do it in this way in
a transition period). Now I would like be able to save from the program and
wish to switch to object serialization, but I'm concerned about backward
compatibility if my classes that are serialized on disk is constantly
extended.

If I extend a class with a field and try to desirialize this class from a
source which did not have the field, I guess I'll get an exception, Right?.
Are there any attributes that can provide some kind of default values for
fields or indicate that it is 'OK!' that the field wasn't found.

Must a class match exactly the fields.

Alternatively I would use xml and write my own serialization and
deserialization methods. Rather cumbersome but maybe necessary if I want this
degree of freedom.

Another way could be to keep the old framework and then parse information
from this old framework to the new one - like when you open a word 97
document in word 2003.

Please comment.

best regards Jesper, Denmark.

Nov 16 '05 #1
10 1379
Did you ever find a solution to this problem? I have a similar issue and am
looking for a solution.
--
Steve
"Jesper" wrote:
Hi,

I'm writing a program which is, and will continue to be, constantly extended
with functionality that require information. Until now the source of this
information has been found and modified using notepad in a txt file that was
parsed into fields in objects. (This solution because this was the format
that an older fortran program was using and we needed to do it in this way in
a transition period). Now I would like be able to save from the program and
wish to switch to object serialization, but I'm concerned about backward
compatibility if my classes that are serialized on disk is constantly
extended.

If I extend a class with a field and try to desirialize this class from a
source which did not have the field, I guess I'll get an exception, Right?.
Are there any attributes that can provide some kind of default values for
fields or indicate that it is 'OK!' that the field wasn't found.

Must a class match exactly the fields.

Alternatively I would use xml and write my own serialization and
deserialization methods. Rather cumbersome but maybe necessary if I want this
degree of freedom.

Another way could be to keep the old framework and then parse information
from this old framework to the new one - like when you open a word 97
document in word 2003.

Please comment.

best regards Jesper, Denmark.

Nov 17 '05 #2
Don't know if this solves your problem, but you could use one of the classes
that implements IDictionary interface, and is serializable. The keys would
be strings that represented the field named. The values would be objects
that you would have to cast to the correct type when you pulled them from
the dictionary. If a field was not found it could be added with a default
value (and saved when the dictionary was written out). Accessing a field
through a dictionary in this way is rather expensive in CPU time, though.

If you did not want to pay the cost to access the "fields" from the
dictionary, each time they are needed, you could make a constructor for your
extensible class(es). The constructor would read the serialized dictionary
from the file. For each field in the class, it would attempt to fetch it
from the dictionary; if found it would assign it from the dictionary to the
field in the object. If not found, it would initialize it appropriately.
After construction, access to the fields is just from the object.

When your extensible class changes, you have to recompile the new class
definition and change the constructor. However, the old serialized
dictionary will be read and the new version of the class initialized with
the existing fields from the dictionary. The new fields would be defaulted
by the new constructor code you wrote, when/if they were not found in the
dictionary.

Some part of your application would write out the serializable dictionary,
after updating the key/values in the dictionary from the object you were
using.

This is about the same as saving XML and parsing it to build an object of
"extensible class", but use of the dictionary saves you the trouble of
writing parsing logic.

You could have separate dictionaries and files for each extensible class,
but it might be easier to have one serializable dictionary. You could
easily qualify the field names (strings) with the class, as in "foo.in",
"bar.in", and use them to build the "bar" and "foo" objects appropriately.

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
Did you ever find a solution to this problem? I have a similar issue and
am
looking for a solution.
--
Steve
"Jesper" wrote:
Hi,

I'm writing a program which is, and will continue to be, constantly
extended
with functionality that require information. Until now the source of this
information has been found and modified using notepad in a txt file that
was
parsed into fields in objects. (This solution because this was the format
that an older fortran program was using and we needed to do it in this
way in
a transition period). Now I would like be able to save from the program
and
wish to switch to object serialization, but I'm concerned about backward
compatibility if my classes that are serialized on disk is constantly
extended.

If I extend a class with a field and try to desirialize this class from a
source which did not have the field, I guess I'll get an exception,
Right?.
Are there any attributes that can provide some kind of default values for
fields or indicate that it is 'OK!' that the field wasn't found.

Must a class match exactly the fields.

Alternatively I would use xml and write my own serialization and
deserialization methods. Rather cumbersome but maybe necessary if I want
this
degree of freedom.

Another way could be to keep the old framework and then parse information
from this old framework to the new one - like when you open a word 97
document in word 2003.

Please comment.

best regards Jesper, Denmark.

Nov 17 '05 #3
This is the best solution I've seen proposed yet. It appears at least a half
a dozen other people have a question similar to mine but I've not yet seen a
solution proposed to any of their posts. As for the proposal, it will
require time to re-code it this way, but I think it would work. Thanks for
the idea.

--
Steve
"Fred Mellender" wrote:
Don't know if this solves your problem, but you could use one of the classes
that implements IDictionary interface, and is serializable. The keys would
be strings that represented the field named. The values would be objects
that you would have to cast to the correct type when you pulled them from
the dictionary. If a field was not found it could be added with a default
value (and saved when the dictionary was written out). Accessing a field
through a dictionary in this way is rather expensive in CPU time, though.

If you did not want to pay the cost to access the "fields" from the
dictionary, each time they are needed, you could make a constructor for your
extensible class(es). The constructor would read the serialized dictionary
from the file. For each field in the class, it would attempt to fetch it
from the dictionary; if found it would assign it from the dictionary to the
field in the object. If not found, it would initialize it appropriately.
After construction, access to the fields is just from the object.

When your extensible class changes, you have to recompile the new class
definition and change the constructor. However, the old serialized
dictionary will be read and the new version of the class initialized with
the existing fields from the dictionary. The new fields would be defaulted
by the new constructor code you wrote, when/if they were not found in the
dictionary.

Some part of your application would write out the serializable dictionary,
after updating the key/values in the dictionary from the object you were
using.

This is about the same as saving XML and parsing it to build an object of
"extensible class", but use of the dictionary saves you the trouble of
writing parsing logic.

You could have separate dictionaries and files for each extensible class,
but it might be easier to have one serializable dictionary. You could
easily qualify the field names (strings) with the class, as in "foo.in",
"bar.in", and use them to build the "bar" and "foo" objects appropriately.

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
Did you ever find a solution to this problem? I have a similar issue and
am
looking for a solution.
--
Steve
"Jesper" wrote:
Hi,

I'm writing a program which is, and will continue to be, constantly
extended
with functionality that require information. Until now the source of this
information has been found and modified using notepad in a txt file that
was
parsed into fields in objects. (This solution because this was the format
that an older fortran program was using and we needed to do it in this
way in
a transition period). Now I would like be able to save from the program
and
wish to switch to object serialization, but I'm concerned about backward
compatibility if my classes that are serialized on disk is constantly
extended.

If I extend a class with a field and try to desirialize this class from a
source which did not have the field, I guess I'll get an exception,
Right?.
Are there any attributes that can provide some kind of default values for
fields or indicate that it is 'OK!' that the field wasn't found.

Must a class match exactly the fields.

Alternatively I would use xml and write my own serialization and
deserialization methods. Rather cumbersome but maybe necessary if I want
this
degree of freedom.

Another way could be to keep the old framework and then parse information
from this old framework to the new one - like when you open a word 97
document in word 2003.

Please comment.

best regards Jesper, Denmark.


Nov 17 '05 #4
Fred,
FYI... I implemented your proposed solution and it worked. I now write data
to a dictionary and write it to disk. I then recall the dictionary, look for
keys, and repopulate my test structures only if the key was found. It was
trick creating a dictionary that stored any object with a cooresponding key
but it is working. Thanks for the suggestion.
--
Steve
"Fred Mellender" wrote:
Don't know if this solves your problem, but you could use one of the classes
that implements IDictionary interface, and is serializable. The keys would
be strings that represented the field named. The values would be objects
that you would have to cast to the correct type when you pulled them from
the dictionary. If a field was not found it could be added with a default
value (and saved when the dictionary was written out). Accessing a field
through a dictionary in this way is rather expensive in CPU time, though.

If you did not want to pay the cost to access the "fields" from the
dictionary, each time they are needed, you could make a constructor for your
extensible class(es). The constructor would read the serialized dictionary
from the file. For each field in the class, it would attempt to fetch it
from the dictionary; if found it would assign it from the dictionary to the
field in the object. If not found, it would initialize it appropriately.
After construction, access to the fields is just from the object.

When your extensible class changes, you have to recompile the new class
definition and change the constructor. However, the old serialized
dictionary will be read and the new version of the class initialized with
the existing fields from the dictionary. The new fields would be defaulted
by the new constructor code you wrote, when/if they were not found in the
dictionary.

Some part of your application would write out the serializable dictionary,
after updating the key/values in the dictionary from the object you were
using.

This is about the same as saving XML and parsing it to build an object of
"extensible class", but use of the dictionary saves you the trouble of
writing parsing logic.

You could have separate dictionaries and files for each extensible class,
but it might be easier to have one serializable dictionary. You could
easily qualify the field names (strings) with the class, as in "foo.in",
"bar.in", and use them to build the "bar" and "foo" objects appropriately.

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
Did you ever find a solution to this problem? I have a similar issue and
am
looking for a solution.
--
Steve
"Jesper" wrote:
Hi,

I'm writing a program which is, and will continue to be, constantly
extended
with functionality that require information. Until now the source of this
information has been found and modified using notepad in a txt file that
was
parsed into fields in objects. (This solution because this was the format
that an older fortran program was using and we needed to do it in this
way in
a transition period). Now I would like be able to save from the program
and
wish to switch to object serialization, but I'm concerned about backward
compatibility if my classes that are serialized on disk is constantly
extended.

If I extend a class with a field and try to desirialize this class from a
source which did not have the field, I guess I'll get an exception,
Right?.
Are there any attributes that can provide some kind of default values for
fields or indicate that it is 'OK!' that the field wasn't found.

Must a class match exactly the fields.

Alternatively I would use xml and write my own serialization and
deserialization methods. Rather cumbersome but maybe necessary if I want
this
degree of freedom.

Another way could be to keep the old framework and then parse information
from this old framework to the new one - like when you open a word 97
document in word 2003.

Please comment.

best regards Jesper, Denmark.


Nov 17 '05 #5
Hi steeve

I am facing the same problem. Kindly send me sample code or workarounds for
this problem.

Thanks

"Steve Teeples" wrote:
Fred,
FYI... I implemented your proposed solution and it worked. I now write data
to a dictionary and write it to disk. I then recall the dictionary, look for
keys, and repopulate my test structures only if the key was found. It was
trick creating a dictionary that stored any object with a cooresponding key
but it is working. Thanks for the suggestion.
--
Steve
"Fred Mellender" wrote:
Don't know if this solves your problem, but you could use one of the classes
that implements IDictionary interface, and is serializable. The keys would
be strings that represented the field named. The values would be objects
that you would have to cast to the correct type when you pulled them from
the dictionary. If a field was not found it could be added with a default
value (and saved when the dictionary was written out). Accessing a field
through a dictionary in this way is rather expensive in CPU time, though.

If you did not want to pay the cost to access the "fields" from the
dictionary, each time they are needed, you could make a constructor for your
extensible class(es). The constructor would read the serialized dictionary
from the file. For each field in the class, it would attempt to fetch it
from the dictionary; if found it would assign it from the dictionary to the
field in the object. If not found, it would initialize it appropriately.
After construction, access to the fields is just from the object.

When your extensible class changes, you have to recompile the new class
definition and change the constructor. However, the old serialized
dictionary will be read and the new version of the class initialized with
the existing fields from the dictionary. The new fields would be defaulted
by the new constructor code you wrote, when/if they were not found in the
dictionary.

Some part of your application would write out the serializable dictionary,
after updating the key/values in the dictionary from the object you were
using.

This is about the same as saving XML and parsing it to build an object of
"extensible class", but use of the dictionary saves you the trouble of
writing parsing logic.

You could have separate dictionaries and files for each extensible class,
but it might be easier to have one serializable dictionary. You could
easily qualify the field names (strings) with the class, as in "foo.in",
"bar.in", and use them to build the "bar" and "foo" objects appropriately.

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
Did you ever find a solution to this problem? I have a similar issue and
am
looking for a solution.
--
Steve
"Jesper" wrote:

> Hi,
>
> I'm writing a program which is, and will continue to be, constantly
> extended
> with functionality that require information. Until now the source of this
> information has been found and modified using notepad in a txt file that
> was
> parsed into fields in objects. (This solution because this was the format
> that an older fortran program was using and we needed to do it in this
> way in
> a transition period). Now I would like be able to save from the program
> and
> wish to switch to object serialization, but I'm concerned about backward
> compatibility if my classes that are serialized on disk is constantly
> extended.
>
> If I extend a class with a field and try to desirialize this class from a
> source which did not have the field, I guess I'll get an exception,
> Right?.
> Are there any attributes that can provide some kind of default values for
> fields or indicate that it is 'OK!' that the field wasn't found.
>
> Must a class match exactly the fields.
>
> Alternatively I would use xml and write my own serialization and
> deserialization methods. Rather cumbersome but maybe necessary if I want
> this
> degree of freedom.
>
> Another way could be to keep the old framework and then parse information
> from this old framework to the new one - like when you open a word 97
> document in word 2003.
>
> Please comment.
>
> best regards Jesper, Denmark.
>


Nov 17 '05 #6
Hi steeve

I am facing the same problem. Kindly send me sample code or workarounds for
this problem.

Thanks

"Steve Teeples" wrote:
Fred,
FYI... I implemented your proposed solution and it worked. I now write data
to a dictionary and write it to disk. I then recall the dictionary, look for
keys, and repopulate my test structures only if the key was found. It was
trick creating a dictionary that stored any object with a cooresponding key
but it is working. Thanks for the suggestion.
--
Steve
"Fred Mellender" wrote:
Don't know if this solves your problem, but you could use one of the classes
that implements IDictionary interface, and is serializable. The keys would
be strings that represented the field named. The values would be objects
that you would have to cast to the correct type when you pulled them from
the dictionary. If a field was not found it could be added with a default
value (and saved when the dictionary was written out). Accessing a field
through a dictionary in this way is rather expensive in CPU time, though.

If you did not want to pay the cost to access the "fields" from the
dictionary, each time they are needed, you could make a constructor for your
extensible class(es). The constructor would read the serialized dictionary
from the file. For each field in the class, it would attempt to fetch it
from the dictionary; if found it would assign it from the dictionary to the
field in the object. If not found, it would initialize it appropriately.
After construction, access to the fields is just from the object.

When your extensible class changes, you have to recompile the new class
definition and change the constructor. However, the old serialized
dictionary will be read and the new version of the class initialized with
the existing fields from the dictionary. The new fields would be defaulted
by the new constructor code you wrote, when/if they were not found in the
dictionary.

Some part of your application would write out the serializable dictionary,
after updating the key/values in the dictionary from the object you were
using.

This is about the same as saving XML and parsing it to build an object of
"extensible class", but use of the dictionary saves you the trouble of
writing parsing logic.

You could have separate dictionaries and files for each extensible class,
but it might be easier to have one serializable dictionary. You could
easily qualify the field names (strings) with the class, as in "foo.in",
"bar.in", and use them to build the "bar" and "foo" objects appropriately.

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
Did you ever find a solution to this problem? I have a similar issue and
am
looking for a solution.
--
Steve
"Jesper" wrote:

> Hi,
>
> I'm writing a program which is, and will continue to be, constantly
> extended
> with functionality that require information. Until now the source of this
> information has been found and modified using notepad in a txt file that
> was
> parsed into fields in objects. (This solution because this was the format
> that an older fortran program was using and we needed to do it in this
> way in
> a transition period). Now I would like be able to save from the program
> and
> wish to switch to object serialization, but I'm concerned about backward
> compatibility if my classes that are serialized on disk is constantly
> extended.
>
> If I extend a class with a field and try to desirialize this class from a
> source which did not have the field, I guess I'll get an exception,
> Right?.
> Are there any attributes that can provide some kind of default values for
> fields or indicate that it is 'OK!' that the field wasn't found.
>
> Must a class match exactly the fields.
>
> Alternatively I would use xml and write my own serialization and
> deserialization methods. Rather cumbersome but maybe necessary if I want
> this
> degree of freedom.
>
> Another way could be to keep the old framework and then parse information
> from this old framework to the new one - like when you open a word 97
> document in word 2003.
>
> Please comment.
>
> best regards Jesper, Denmark.
>


Nov 17 '05 #7
Hi steeve

I am facing the same problem. Kindly send me sample code or workarounds for
this problem.

Thanks

"Steve Teeples" wrote:
Fred,
FYI... I implemented your proposed solution and it worked. I now write data
to a dictionary and write it to disk. I then recall the dictionary, look for
keys, and repopulate my test structures only if the key was found. It was
trick creating a dictionary that stored any object with a cooresponding key
but it is working. Thanks for the suggestion.
--
Steve
"Fred Mellender" wrote:
Don't know if this solves your problem, but you could use one of the classes
that implements IDictionary interface, and is serializable. The keys would
be strings that represented the field named. The values would be objects
that you would have to cast to the correct type when you pulled them from
the dictionary. If a field was not found it could be added with a default
value (and saved when the dictionary was written out). Accessing a field
through a dictionary in this way is rather expensive in CPU time, though.

If you did not want to pay the cost to access the "fields" from the
dictionary, each time they are needed, you could make a constructor for your
extensible class(es). The constructor would read the serialized dictionary
from the file. For each field in the class, it would attempt to fetch it
from the dictionary; if found it would assign it from the dictionary to the
field in the object. If not found, it would initialize it appropriately.
After construction, access to the fields is just from the object.

When your extensible class changes, you have to recompile the new class
definition and change the constructor. However, the old serialized
dictionary will be read and the new version of the class initialized with
the existing fields from the dictionary. The new fields would be defaulted
by the new constructor code you wrote, when/if they were not found in the
dictionary.

Some part of your application would write out the serializable dictionary,
after updating the key/values in the dictionary from the object you were
using.

This is about the same as saving XML and parsing it to build an object of
"extensible class", but use of the dictionary saves you the trouble of
writing parsing logic.

You could have separate dictionaries and files for each extensible class,
but it might be easier to have one serializable dictionary. You could
easily qualify the field names (strings) with the class, as in "foo.in",
"bar.in", and use them to build the "bar" and "foo" objects appropriately.

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
Did you ever find a solution to this problem? I have a similar issue and
am
looking for a solution.
--
Steve
"Jesper" wrote:

> Hi,
>
> I'm writing a program which is, and will continue to be, constantly
> extended
> with functionality that require information. Until now the source of this
> information has been found and modified using notepad in a txt file that
> was
> parsed into fields in objects. (This solution because this was the format
> that an older fortran program was using and we needed to do it in this
> way in
> a transition period). Now I would like be able to save from the program
> and
> wish to switch to object serialization, but I'm concerned about backward
> compatibility if my classes that are serialized on disk is constantly
> extended.
>
> If I extend a class with a field and try to desirialize this class from a
> source which did not have the field, I guess I'll get an exception,
> Right?.
> Are there any attributes that can provide some kind of default values for
> fields or indicate that it is 'OK!' that the field wasn't found.
>
> Must a class match exactly the fields.
>
> Alternatively I would use xml and write my own serialization and
> deserialization methods. Rather cumbersome but maybe necessary if I want
> this
> degree of freedom.
>
> Another way could be to keep the old framework and then parse information
> from this old framework to the new one - like when you open a word 97
> document in word 2003.
>
> Please comment.
>
> best regards Jesper, Denmark.
>


Nov 17 '05 #8
Hi steeve

I am facing the same problem. Kindly send me sample code or workarounds for
this problem.

Thanks

"Steve Teeples" wrote:
Fred,
FYI... I implemented your proposed solution and it worked. I now write data
to a dictionary and write it to disk. I then recall the dictionary, look for
keys, and repopulate my test structures only if the key was found. It was
trick creating a dictionary that stored any object with a cooresponding key
but it is working. Thanks for the suggestion.
--
Steve
"Fred Mellender" wrote:
Don't know if this solves your problem, but you could use one of the classes
that implements IDictionary interface, and is serializable. The keys would
be strings that represented the field named. The values would be objects
that you would have to cast to the correct type when you pulled them from
the dictionary. If a field was not found it could be added with a default
value (and saved when the dictionary was written out). Accessing a field
through a dictionary in this way is rather expensive in CPU time, though.

If you did not want to pay the cost to access the "fields" from the
dictionary, each time they are needed, you could make a constructor for your
extensible class(es). The constructor would read the serialized dictionary
from the file. For each field in the class, it would attempt to fetch it
from the dictionary; if found it would assign it from the dictionary to the
field in the object. If not found, it would initialize it appropriately.
After construction, access to the fields is just from the object.

When your extensible class changes, you have to recompile the new class
definition and change the constructor. However, the old serialized
dictionary will be read and the new version of the class initialized with
the existing fields from the dictionary. The new fields would be defaulted
by the new constructor code you wrote, when/if they were not found in the
dictionary.

Some part of your application would write out the serializable dictionary,
after updating the key/values in the dictionary from the object you were
using.

This is about the same as saving XML and parsing it to build an object of
"extensible class", but use of the dictionary saves you the trouble of
writing parsing logic.

You could have separate dictionaries and files for each extensible class,
but it might be easier to have one serializable dictionary. You could
easily qualify the field names (strings) with the class, as in "foo.in",
"bar.in", and use them to build the "bar" and "foo" objects appropriately.

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
Did you ever find a solution to this problem? I have a similar issue and
am
looking for a solution.
--
Steve
"Jesper" wrote:

> Hi,
>
> I'm writing a program which is, and will continue to be, constantly
> extended
> with functionality that require information. Until now the source of this
> information has been found and modified using notepad in a txt file that
> was
> parsed into fields in objects. (This solution because this was the format
> that an older fortran program was using and we needed to do it in this
> way in
> a transition period). Now I would like be able to save from the program
> and
> wish to switch to object serialization, but I'm concerned about backward
> compatibility if my classes that are serialized on disk is constantly
> extended.
>
> If I extend a class with a field and try to desirialize this class from a
> source which did not have the field, I guess I'll get an exception,
> Right?.
> Are there any attributes that can provide some kind of default values for
> fields or indicate that it is 'OK!' that the field wasn't found.
>
> Must a class match exactly the fields.
>
> Alternatively I would use xml and write my own serialization and
> deserialization methods. Rather cumbersome but maybe necessary if I want
> this
> degree of freedom.
>
> Another way could be to keep the old framework and then parse information
> from this old framework to the new one - like when you open a word 97
> document in word 2003.
>
> Please comment.
>
> best regards Jesper, Denmark.
>


Nov 17 '05 #9
Hi. I was out on vacation so this may be coming to you too late. What I did
is below.

I created a dictionary class that saves any component of type "Object". The
key for each object is unique. The value is of type Object.

I followed a convention of having the namespace, module and field as the
unique name.

Ex. private readonly string strKey4 =
"TestingPowerCycle.TestingPowerCycling.SecondsBefo rePowerCycle";

The method to save the data is below.
/// <summary>
/// Save the data to the dictionary.
/// </summary>
/// <param name="mod">DictionaryObjects.MyObjectDictionary where data is
stored.</param>
public override void SaveData(ref DictionaryObjects.MyObjectDictionary mod)
{
mod.Add(this.strKey4, this.SecondsBeforePowerCycle.ToString());
}

The method to retrieve data is below.

/// <summary>
/// Retrieve data from the dictionary.
/// </summary>
/// <param name="mod">DictionaryObjects.MyObjectDictionary where data was
stored.</param>
public override void RetrieveData(DictionaryObjects.MyObjectDictionary mod)
{
if (mod.Contains(this.strKey4))
this.SecondsBeforePowerCycle = Convert.ToInt32((string)mod[this.strKey4],
10);
}

When streaming the data to the disk I would call similar routines for each
class, save the data to the dictionary, then stream the dictionary to the
disk. You just need to remember that the key must be unique.

Hope this helps.
--
Steve
"Rohith" wrote:
Hi steeve

I am facing the same problem. Kindly send me sample code or workarounds for
this problem.

Thanks

"Steve Teeples" wrote:
Fred,
FYI... I implemented your proposed solution and it worked. I now write data
to a dictionary and write it to disk. I then recall the dictionary, look for
keys, and repopulate my test structures only if the key was found. It was
trick creating a dictionary that stored any object with a cooresponding key
but it is working. Thanks for the suggestion.
--
Steve
"Fred Mellender" wrote:
Don't know if this solves your problem, but you could use one of the classes
that implements IDictionary interface, and is serializable. The keys would
be strings that represented the field named. The values would be objects
that you would have to cast to the correct type when you pulled them from
the dictionary. If a field was not found it could be added with a default
value (and saved when the dictionary was written out). Accessing a field
through a dictionary in this way is rather expensive in CPU time, though.

If you did not want to pay the cost to access the "fields" from the
dictionary, each time they are needed, you could make a constructor for your
extensible class(es). The constructor would read the serialized dictionary
from the file. For each field in the class, it would attempt to fetch it
from the dictionary; if found it would assign it from the dictionary to the
field in the object. If not found, it would initialize it appropriately.
After construction, access to the fields is just from the object.

When your extensible class changes, you have to recompile the new class
definition and change the constructor. However, the old serialized
dictionary will be read and the new version of the class initialized with
the existing fields from the dictionary. The new fields would be defaulted
by the new constructor code you wrote, when/if they were not found in the
dictionary.

Some part of your application would write out the serializable dictionary,
after updating the key/values in the dictionary from the object you were
using.

This is about the same as saving XML and parsing it to build an object of
"extensible class", but use of the dictionary saves you the trouble of
writing parsing logic.

You could have separate dictionaries and files for each extensible class,
but it might be easier to have one serializable dictionary. You could
easily qualify the field names (strings) with the class, as in "foo.in",
"bar.in", and use them to build the "bar" and "foo" objects appropriately.

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
> Did you ever find a solution to this problem? I have a similar issue and
> am
> looking for a solution.
> --
> Steve
>
>
> "Jesper" wrote:
>
>> Hi,
>>
>> I'm writing a program which is, and will continue to be, constantly
>> extended
>> with functionality that require information. Until now the source of this
>> information has been found and modified using notepad in a txt file that
>> was
>> parsed into fields in objects. (This solution because this was the format
>> that an older fortran program was using and we needed to do it in this
>> way in
>> a transition period). Now I would like be able to save from the program
>> and
>> wish to switch to object serialization, but I'm concerned about backward
>> compatibility if my classes that are serialized on disk is constantly
>> extended.
>>
>> If I extend a class with a field and try to desirialize this class from a
>> source which did not have the field, I guess I'll get an exception,
>> Right?.
>> Are there any attributes that can provide some kind of default values for
>> fields or indicate that it is 'OK!' that the field wasn't found.
>>
>> Must a class match exactly the fields.
>>
>> Alternatively I would use xml and write my own serialization and
>> deserialization methods. Rather cumbersome but maybe necessary if I want
>> this
>> degree of freedom.
>>
>> Another way could be to keep the old framework and then parse information
>> from this old framework to the new one - like when you open a word 97
>> document in word 2003.
>>
>> Please comment.
>>
>> best regards Jesper, Denmark.
>>

Nov 17 '05 #10
Steeve,

But this doesnt work for the files already serialized. I want to deserialize
a file that was already saved. Anyway i got up with a a solution. I used
Iserializable interface and it worked.

Thanks

"Steve Teeples" wrote:
Hi. I was out on vacation so this may be coming to you too late. What I did
is below.

I created a dictionary class that saves any component of type "Object". The
key for each object is unique. The value is of type Object.

I followed a convention of having the namespace, module and field as the
unique name.

Ex. private readonly string strKey4 =
"TestingPowerCycle.TestingPowerCycling.SecondsBefo rePowerCycle";

The method to save the data is below.
/// <summary>
/// Save the data to the dictionary.
/// </summary>
/// <param name="mod">DictionaryObjects.MyObjectDictionary where data is
stored.</param>
public override void SaveData(ref DictionaryObjects.MyObjectDictionary mod)
{
mod.Add(this.strKey4, this.SecondsBeforePowerCycle.ToString());
}

The method to retrieve data is below.

/// <summary>
/// Retrieve data from the dictionary.
/// </summary>
/// <param name="mod">DictionaryObjects.MyObjectDictionary where data was
stored.</param>
public override void RetrieveData(DictionaryObjects.MyObjectDictionary mod)
{
if (mod.Contains(this.strKey4))
this.SecondsBeforePowerCycle = Convert.ToInt32((string)mod[this.strKey4],
10);
}

When streaming the data to the disk I would call similar routines for each
class, save the data to the dictionary, then stream the dictionary to the
disk. You just need to remember that the key must be unique.

Hope this helps.
--
Steve
"Rohith" wrote:
Hi steeve

I am facing the same problem. Kindly send me sample code or workarounds for
this problem.

Thanks

"Steve Teeples" wrote:
Fred,
FYI... I implemented your proposed solution and it worked. I now write data
to a dictionary and write it to disk. I then recall the dictionary, look for
keys, and repopulate my test structures only if the key was found. It was
trick creating a dictionary that stored any object with a cooresponding key
but it is working. Thanks for the suggestion.
--
Steve
"Fred Mellender" wrote:

> Don't know if this solves your problem, but you could use one of the classes
> that implements IDictionary interface, and is serializable. The keys would
> be strings that represented the field named. The values would be objects
> that you would have to cast to the correct type when you pulled them from
> the dictionary. If a field was not found it could be added with a default
> value (and saved when the dictionary was written out). Accessing a field
> through a dictionary in this way is rather expensive in CPU time, though.
>
> If you did not want to pay the cost to access the "fields" from the
> dictionary, each time they are needed, you could make a constructor for your
> extensible class(es). The constructor would read the serialized dictionary
> from the file. For each field in the class, it would attempt to fetch it
> from the dictionary; if found it would assign it from the dictionary to the
> field in the object. If not found, it would initialize it appropriately.
> After construction, access to the fields is just from the object.
>
> When your extensible class changes, you have to recompile the new class
> definition and change the constructor. However, the old serialized
> dictionary will be read and the new version of the class initialized with
> the existing fields from the dictionary. The new fields would be defaulted
> by the new constructor code you wrote, when/if they were not found in the
> dictionary.
>
> Some part of your application would write out the serializable dictionary,
> after updating the key/values in the dictionary from the object you were
> using.
>
> This is about the same as saving XML and parsing it to build an object of
> "extensible class", but use of the dictionary saves you the trouble of
> writing parsing logic.
>
> You could have separate dictionaries and files for each extensible class,
> but it might be easier to have one serializable dictionary. You could
> easily qualify the field names (strings) with the class, as in "foo.in",
> "bar.in", and use them to build the "bar" and "foo" objects appropriately.
>
>
>
> "Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
> news:55**********************************@microsof t.com...
> > Did you ever find a solution to this problem? I have a similar issue and
> > am
> > looking for a solution.
> > --
> > Steve
> >
> >
> > "Jesper" wrote:
> >
> >> Hi,
> >>
> >> I'm writing a program which is, and will continue to be, constantly
> >> extended
> >> with functionality that require information. Until now the source of this
> >> information has been found and modified using notepad in a txt file that
> >> was
> >> parsed into fields in objects. (This solution because this was the format
> >> that an older fortran program was using and we needed to do it in this
> >> way in
> >> a transition period). Now I would like be able to save from the program
> >> and
> >> wish to switch to object serialization, but I'm concerned about backward
> >> compatibility if my classes that are serialized on disk is constantly
> >> extended.
> >>
> >> If I extend a class with a field and try to desirialize this class from a
> >> source which did not have the field, I guess I'll get an exception,
> >> Right?.
> >> Are there any attributes that can provide some kind of default values for
> >> fields or indicate that it is 'OK!' that the field wasn't found.
> >>
> >> Must a class match exactly the fields.
> >>
> >> Alternatively I would use xml and write my own serialization and
> >> deserialization methods. Rather cumbersome but maybe necessary if I want
> >> this
> >> degree of freedom.
> >>
> >> Another way could be to keep the old framework and then parse information
> >> from this old framework to the new one - like when you open a word 97
> >> document in word 2003.
> >>
> >> Please comment.
> >>
> >> best regards Jesper, Denmark.
> >>
>
>
>

Nov 17 '05 #11

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

Similar topics

0
by: Dave | last post by:
I was curious to know if anyone has suggestions about maintaining various versions of class definitions when using serialization to save data. Is there a "standard" way of handling backward...
9
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these...
0
by: HakonB | last post by:
Hi all I get an exception when trying to deserialize a simple configuration file using XML Serialization. The very long stacktrace can be seen at the bottom of this message. I've see other...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
5
by: Nikola Skoric | last post by:
I ran in Mono a program developed on .NET Framework 2.0 and it ran OK until I tried to desirialize a object. There the program died abruptly dumping this: System.ArgumentOutOfRangeException:...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.