Parse and ToString?

P

Peter K

Hi

I have an object which I need to store in a "state holder" for the
application the object is to be used in. The "state holder" can only store
strings. (I cannot change this code and I must use it).

So I need to convert my object to a string to store in the state holder,
and provide a function to convert from a string to my object.

What are some good ways of approaching this?

Luckily, my object is very simple - it is really only a data container with
a few strings and numeric values. At the moment I have implemented a
"helper" class which reads my objects public properties and generates a
comma-separated string which I store in the state holder; and conversely a
conversion method the other way (which takes the comma separated string and
instantiates my object from that).

But maybe it would be better to incorporate this functionality in the
object itself? So I have a "ToString()", and a static "Parse" method or
something?

Thanks for any ideas,
Peter
 
G

grava

Peter K said:
Hi

I have an object which I need to store in a "state holder" for the
application the object is to be used in. The "state holder" can only store
strings. (I cannot change this code and I must use it).

So I need to convert my object to a string to store in the state holder,
and provide a function to convert from a string to my object.

What are some good ways of approaching this?

Luckily, my object is very simple - it is really only a data container
with
a few strings and numeric values. At the moment I have implemented a
"helper" class which reads my objects public properties and generates a
comma-separated string which I store in the state holder; and conversely a
conversion method the other way (which takes the comma separated string
and
instantiates my object from that).

But maybe it would be better to incorporate this functionality in the
object itself? So I have a "ToString()", and a static "Parse" method or
something?

Thanks for any ideas,
Peter

What about serialize your object with XmlSerializer ?


http://msdn2.microsoft.com/en-us/library/szzyf24s(VS.80).aspx
 

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