Why does 20, 20 not "ConvertFrom" in Norwegian, but does in English?

  • Thread starter Thread starter Robin Tucker
  • Start date Start date
R

Robin Tucker

(note, this is a C# or VB.NET ambivalent question....)


Hi,

Have a slight problem with a library I'm using here (which I have the source
code for). It has data files serialized with an English ("en") locale, but
I'm trying to de-serialize with a Norwegian ("no") locale. The issue is
with the SizeConvertor class. Consider:

protected static SizeConverter _sc = new SizeConverter();

public static Size StringToSize(string str)
{
return (Size)_sc.ConvertFrom(str);
}

When str = "20, 20" and using `en' locale, the result is a returned Size of
20, 20. However, when using the `no' Norwegian locale, the result is a
thrown exception:

`Message "20 is not a valid value for Int32." String'

The inner exception says:

`Input string was not in a correct format.'




Now, if I change the above to:



public static Size StringToSize(string str)
{
return (Size)_sc.ConvertFrom(null, new
System.Globalization.CultureInfo("en"), str);
}


, in order to get a conversion using the locale the file was created with, I
get a different [inner] exception:

'Culture `en' is a neutral culture. It can not be used in formatting and
parsing and therefore cannot be set as the thread's current culture.'



Can anyone tell me what gives here? How can I read resources created with
"en" back in with "en" parsing when using Norwegian (or
_insert_other_ui_locale_here_)?



Thanks,




Robin
 
Hello Robin,

That happens because the comma is the decimal separator in Norwegian. In that circumstance, the comma is not a valid list separator for numbers, as the conversion is seeing something like "20. 20" in English, which is not a valid number.

Using InvariantCulture or US culture, wich is the same, allows you to use the same string in any culture, as you discovered.

Regards.


"Robin Tucker" <[email protected]> escribió en el mensaje |I got it - I need to use System.Globalization.CultureInfo.InvariantCulture
| :))
|
| | > (note, this is a C# or VB.NET ambivalent question....)
| >
| >
| > Hi,
| >
| > Have a slight problem with a library I'm using here (which I have the
| > source code for). It has data files serialized with an English ("en")
| > locale, but I'm trying to de-serialize with a Norwegian ("no") locale.
| > The issue is with the SizeConvertor class. Consider:
| >
| > protected static SizeConverter _sc = new SizeConverter();
| >
| > public static Size StringToSize(string str)
| > {
| > return (Size)_sc.ConvertFrom(str);
| > }
| >
| > When str = "20, 20" and using `en' locale, the result is a returned Size
| > of 20, 20. However, when using the `no' Norwegian locale, the result is a
| > thrown exception:
| >
| > `Message "20 is not a valid value for Int32." String'
| >
| > The inner exception says:
| >
| > `Input string was not in a correct format.'
| >
| >
| >
| >
| > Now, if I change the above to:
| >
| >
| >
| > public static Size StringToSize(string str)
| > {
| > return (Size)_sc.ConvertFrom(null, new
| > System.Globalization.CultureInfo("en"), str);
| > }
| >
| >
| > , in order to get a conversion using the locale the file was created with,
| > I get a different [inner] exception:
| >
| > 'Culture `en' is a neutral culture. It can not be used in formatting
| > and parsing and therefore cannot be set as the thread's current culture.'
| >
| >
| >
| > Can anyone tell me what gives here? How can I read resources created with
| > "en" back in with "en" parsing when using Norwegian (or
| > _insert_other_ui_locale_here_)?
| >
| >
| >
| > Thanks,
| >
| >
| >
| >
| > Robin
| >
| >
| >
|
|
 

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

Back
Top