Hi there,
Does anyone know how to go about reading/writing a type to a file in a
language (culture) independent way. For instance, let's say you're dealing
with the native "System.Drawing.Size" type on an English version of Windows.
You use the "TypeConverter" for this structure to generate the string, say,
"50, 75" which you then store in a file (or perhaps a DB). Now, the same
value might later be read back in from this file on a Japenese version of
Windows (or more accurately on a thread where the culture is set to
Japanese). Let's say the comma (",") is actually a period (".") in Japanese
however though I'm just making this up for demonstration purposes. How do
you now convert the original value "50, 75" into a "Size" object given that
the system presumably won't recognize the comma anymore (only a period).
Conversely, how do you convert a "Size" object back to a culture-independent
string (in this case using a comma) so that it can be processed on the
original English machine again. Or maybe this isn't as complicated as I'm
making it out to be. I'm just not sure how to use the "TypeConverter" class
to write my string in a consistent way given that it has to be read back in
on a thread running with a different "CutlureInfo". Can anyone provide any
insight on the matter. Thanks very much.
John Brown - 26 May 2007 18:12 GMT
> Does anyone know how to go about reading/writing a type to a file in a
> language (culture) independent way. For instance, let's say you're dealing
[quoted text clipped - 15 lines]
> with a different "CutlureInfo". Can anyone provide any insight on the
> matter. Thanks very much.
Ok, I'm thinking that I need to call
"TypeConverter.ConvertToInvariantString()" to write the value and
"TypeConverter.ConvertFromInvariantString()" to read it back in. However,
once read back in using the latter function, I then need to pass the value
to "TypeConverter.ConvertToString()" if I actually want to display it (since
I assume "ConvertToString()" is culture-sensitive). Conversely, if a user
enters the string in some field during data-input, I would need to convert
it using "TypeConverter.ConvertFromString()" and then pass this back to
"TypeConverter.ConvertToInvariantString()" before writing it to file again.
Is this correct? Thanks.
pax - 26 May 2007 19:12 GMT
The comma you are talking about is actually part of the language, C#, it is
a list separator, and therefore it's got nothing to do with localization!
>> Does anyone know how to go about reading/writing a type to a file in a
>> language (culture) independent way. For instance, let's say you're
[quoted text clipped - 26 lines]
> back to "TypeConverter.ConvertToInvariantString()" before writing it to
> file again. Is this correct? Thanks.
pax - 26 May 2007 19:32 GMT
Sorry, my bad, it seems that the list separator IS part of the Regional
Options :)
> The comma you are talking about is actually part of the language, C#, it
> is a list separator, and therefore it's got nothing to do with
[quoted text clipped - 30 lines]
>> this back to "TypeConverter.ConvertToInvariantString()" before writing
>> it to file again. Is this correct? Thanks.
Jon Skeet [C# MVP] - 26 May 2007 19:12 GMT
> Does anyone know how to go about reading/writing a type to a file in a
> language (culture) independent way. For instance, let's say you're dealing
[quoted text clipped - 7 lines]
> you now convert the original value "50, 75" into a "Size" object given that
> the system presumably won't recognize the comma anymore (only a period).
Have you tested this assumption? I would hope that TypeConverter was
already culture-independent, but I haven't tried it.
> Conversely, how do you convert a "Size" object back to a culture-independent
> string (in this case using a comma) so that it can be processed on the
[quoted text clipped - 3 lines]
> on a thread running with a different "CutlureInfo". Can anyone provide any
> insight on the matter. Thanks very much.
I'd say the first thing to do is to make sure there's actually a
problem. Try exactly the situation you fear will fail.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
John Brown - 27 May 2007 15:03 GMT
Thanks for the feedback...
>> Does anyone know how to go about reading/writing a type to a file in a
>> language (culture) independent way. For instance, let's say you're
[quoted text clipped - 15 lines]
> Have you tested this assumption? I would hope that TypeConverter was
> already culture-independent, but I haven't tried it.
I can't find any documenation that clarifies the situation but I'm fairly
certain that it isn't (culturally-independent). How could it be for instance
when commas and other types of punctuation can differ from one culture to
another (which is readily apparent simply by looking at regional settings in
the control panel). Moreover, customized type conversions may be influenced
by locality and that's something you can never escape. Finally, a quick look
at some of the "TypeConverter()" functions clearly indicates that culture
does come into play but I can't pin down the precise rules.
>> Conversely, how do you convert a "Size" object back to a
>> culture-independent
[quoted text clipped - 10 lines]
> I'd say the first thing to do is to make sure there's actually a
> problem. Try exactly the situation you fear will fail.
You can't accurately test this however without actually installing a
localized version of Windows or possibly using the Windows MUI (Multi User
Interface) pack which really isn't a (completely) accurate test IMO. The MUI
pack isn't available through normal retail channels in any case. I'd like to
know what the rules actually say rather than just relying on ad hoc tests.
Anyway. thanks again for the feedback.
Jon Skeet [C# MVP] - 27 May 2007 20:54 GMT
> > Have you tested this assumption? I would hope that TypeConverter was
> > already culture-independent, but I haven't tried it.
[quoted text clipped - 7 lines]
> at some of the "TypeConverter()" functions clearly indicates that culture
> does come into play but I can't pin down the precise rules.
My mistake - I'd somehow thought you were talking about a serializer.
However, it certainly does look like the ConvertToInvariantString calls
are what you're after.
> > I'd say the first thing to do is to make sure there's actually a
> > problem. Try exactly the situation you fear will fail.
[quoted text clipped - 5 lines]
> know what the rules actually say rather than just relying on ad hoc tests.
> Anyway. thanks again for the feedback.
Well, just changing the culture of the current thread and seeing
whether you can still retrieve the data accurately would be a good test
to start with, I'd say. I completely understand about wanting more than
tests though :)

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
AlexS - 27 May 2007 02:13 GMT
Can CultureInfo.InvariantCulture property help you to achieve required
result?
> Hi there,
>
[quoted text clipped - 17 lines]
> with a different "CutlureInfo". Can anyone provide any insight on the
> matter. Thanks very much.
John Brown - 27 May 2007 15:05 GMT
> Can CultureInfo.InvariantCulture property help you to achieve required
> result?
Maybe (probably perhaps) but I'm still looking into the matter as mentioned
in my initial follow-up post. I think
"TypeConverter.ConvertToInvariantString()" and cousins are probably the key.
Peter Ritchie [C# MVP] - 27 May 2007 15:26 GMT
Size size = new Size(1,2);
TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(Size));
String text = typeConverter.ConvertToInvariantString(size);
size = (Size)typeConverter.ConvertFromInvariantString(text);
Which is jus short-hand for:
text = typeConverter.ConvertToString(null, CultureInfo.InvariantCulture,
size);
size = (Size)typeConverter.ConvertFromString(null,
CultureInfo.InvariantCulture, text);

Signature
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
> Hi there,
>
[quoted text clipped - 16 lines]
> on a thread running with a different "CutlureInfo". Can anyone provide any
> insight on the matter. Thanks very much.
John Brown - 27 May 2007 17:35 GMT
> Size size = new Size(1,2);
> TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(Size));
[quoted text clipped - 6 lines]
> size = (Size)typeConverter.ConvertFromString(null,
> CultureInfo.InvariantCulture, text);
Ok, thanks. It appears that the use of "InvariantCulture" is probably the
key so at least I can go from here.