locked
Any known issues when set Thread.CurrentThread.CurrentCulture? RRS feed

  • Question

  • User-209105085 posted
    I have a web site whose contents are localized. I’m setting Thread.CurrentThread.CurrentUICulture in InitializeCulture() method and use this CurrentUICulture to get resources. This is working perfectly fine. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>Note: I’m NOT setting Thread.CurrentThread.CurrentCulture in InitializeCulture() method.<o:p></o:p><o:p> </o:p>Now I have text box that shows currency value. This decimal value is coming from database. User has option to update this value. And I save this updated value back to database. Check the code below.<o:p>  </o:p><o:p>
    protected void Page_Load(object sender, EventArgs e)
    {
         TextBox1.Text = string.Format(Thread.CurrentThread.CurrentUICulture, "{0:C}", decimalValueFromDB);
    }
    
    protected void Save_Click(object sender, EventArgs e)
    {
         decimal valueToSaveInDB = decimal.Parse(valueFromTextBox, NumberStyles.Currency, Thread.CurrentThread.CurrentUICulture);            
    }
    This works fine, but notice that I have to pass Thread.CurrentThread.CurrentUICulture for converting value back and forth.<o:p></o:p>

    To do above stuff, my second option is, in InitializeCulture() method also set CurrentCulture and then I don’t have to pass IFormatProvider while converting.  

    What I wanted to know are there any issues if I set CurrentCulture? Because I know .Net uses CurrentCulture for so many other things like

    comparing string.<o:p></o:p>

    </o:p> 

    Wednesday, March 4, 2009 1:21 PM

All replies

  • User1070236303 posted

    The recommended way (fxcop always cry for it) to always supply CultureInfo for any functions which has that parameter.

    Because CurrentUICulture is used only by resources, it is cleaner (for me) to set both currentculture and currentuiculture to the same.

    Now you can write:

    decimal.Parse( valueFromTextBox );
    But this is not liked by Fxcop, to elminate the warning you need:
    decimal.Parse(valueFromTextBox, CultureInfo.CurrentCulture );
    If you want to use the default en-us culture for sorting etc. you can supply CultureInfo.InvariabtCulture for the given method.
    Wednesday, March 11, 2009 4:18 AM
  • User-209105085 posted

     if i have to pass culture then why should i set CurrentCulture. I can set only UIculture and pass the UICulture to decimal.parse()...my question was regarding string comparing..i mean there is some kind of problem i heard while comparing string because of the ASCII representaion of character. do u know anything about it

    Thursday, March 12, 2009 11:18 PM
  • User1070236303 posted

    if i have to pass culture then why should i set CurrentCulture

    Because CurrentUICulture is only for resource look ups. The recommended to set CurrentCulture and CurrentUICulture to the same (in InitializeCulture), that way your users get all resource text and all number, currency etc. formatting automatically for theirs language. You don't need to supply any cultureinfo for parse methods.

    i mean there is some kind of problem i heard while comparing string because of the ASCII representaion of character

    There are string comparing "issues": New Recommendations for Using Strings in Microsoft .NET 2.0 

    (All string is in unicode in the framework, "ascii" is only an encoding.) 

    Friday, March 13, 2009 8:29 AM