Point conversion.
-
Hi, What is the best way to convert a point string to a point object ? Point Pt1 = new Point(10, 20); string strPt = Pt1.ToString(); Point Pt2 = ? (use strPt to convert back to Point object) Thx.
If you have control over both parts of the operation, i.e. this is for persisting some data you own to disk/network/etc, you should use the TypeConverter:
Point pt1 = new Point(10, 20);
string strPt = TypeDescriptor.GetConverter(pt1).ConvertToString(pt1);
Point pt2 = TypeDescriptor.GetConverter(typeof(Point)).ConvertFromString(strPt);If you don't have control over the string format, you will have to write a parser. It's pretty simple (clean off the brackets, split on ',', verify two parts, aand parse both halves as int).
-
If you have control over both parts of the operation, i.e. this is for persisting some data you own to disk/network/etc, you should use the TypeConverter:
Point pt1 = new Point(10, 20);
string strPt = TypeDescriptor.GetConverter(pt1).ConvertToString(pt1);
Point pt2 = TypeDescriptor.GetConverter(typeof(Point)).ConvertFromString(strPt);If you don't have control over the string format, you will have to write a parser. It's pretty simple (clean off the brackets, split on ',', verify two parts, aand parse both halves as int).
-
As an intermediate format without having to resort to serialization :)
Bastard Programmer from Hell :suss:
-
Typically the answer to this is if you want to store something in a human readable form. Object->string conversions are often part of file I/O. (This is what I'm using similar code for.) Or, for a web app, creating a POST request body, though there it might be better to use &x=42&y=72.
-
Typically the answer to this is if you want to store something in a human readable form. Object->string conversions are often part of file I/O. (This is what I'm using similar code for.) Or, for a web app, creating a POST request body, though there it might be better to use &x=42&y=72.
-
True, that's an option too :) You can store every class by simply looping it's public properties, and to store the contents as a string. (Storing both ints in a string is the same thing) That would mean that you have to pay attention to the format, and match up the encode/decode functions. The TypeConverter is a somewhat "safer" way of converting your class, because it adheres to a fixed format for that type in a human-readable form. It's not only used for locations, but also for colors and the like. This would be my recommendation. A third option would be serialization. That'd be a bit overkill in this particular situation, but it remains an option.
Bastard Programmer from Hell :suss:
-
True, that's an option too :) You can store every class by simply looping it's public properties, and to store the contents as a string. (Storing both ints in a string is the same thing) That would mean that you have to pay attention to the format, and match up the encode/decode functions. The TypeConverter is a somewhat "safer" way of converting your class, because it adheres to a fixed format for that type in a human-readable form. It's not only used for locations, but also for colors and the like. This would be my recommendation. A third option would be serialization. That'd be a bit overkill in this particular situation, but it remains an option.
Bastard Programmer from Hell :suss:
-
True, that's an option too :) You can store every class by simply looping it's public properties, and to store the contents as a string. (Storing both ints in a string is the same thing) That would mean that you have to pay attention to the format, and match up the encode/decode functions. The TypeConverter is a somewhat "safer" way of converting your class, because it adheres to a fixed format for that type in a human-readable form. It's not only used for locations, but also for colors and the like. This would be my recommendation. A third option would be serialization. That'd be a bit overkill in this particular situation, but it remains an option.
Bastard Programmer from Hell :suss:
Eddy Vluggen wrote:
You can store every class by simply looping it's public properties, and to store the contents as a string.
Just curious: you mean by using Reflection ? And, if I may ask, why would serialization be "over-kill" in this scenario ... assuming this question is not a one-off, but has broader implications. And, how about using Mehdi Gholam's zippy implementation of JSON on steroids here on CP ? Over-kill ? best, Bill
"Last year I went fishing with Salvador Dali. He was using a dotted line. He caught every other fish." Steven Wright
-
Eddy Vluggen wrote:
You can store every class by simply looping it's public properties, and to store the contents as a string.
Just curious: you mean by using Reflection ? And, if I may ask, why would serialization be "over-kill" in this scenario ... assuming this question is not a one-off, but has broader implications. And, how about using Mehdi Gholam's zippy implementation of JSON on steroids here on CP ? Over-kill ? best, Bill
"Last year I went fishing with Salvador Dali. He was using a dotted line. He caught every other fish." Steven Wright
BillWoodruff wrote:
Just curious: you mean by using Reflection ?
That's what it implies, and yes, I know that you don't need reflection for serialization.
BillWoodruff wrote:
And, if I may ask, why would serialization be "over-kill" in this scenario ... assuming this question is not a one-off, but has broader implications.
Because the TS gave an example where he'd convert a small structure to a string and back. If he had asked how to store a class or a collection, I'd answered serialization.
BillWoodruff wrote:
And, how about using Mehdi Gholam's zippy implementation of JSON on steroids here on CP ? Over-kill ?
I'm not familiar with the article, but it sounds like a viable alternative.
Bastard Programmer from Hell :suss: