Microsoft KB Archive/834064

= Changes that are made to the locale settings of the current thread do not appear in the FormatCurrency function in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 834064

Article Last Modified on 11/27/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-





SYMPTOMS
When you use native Windows application programming interfaces (APIs) to change the locale settings of the current thread in Microsoft Visual Basic .NET or in Microsoft Visual Basic 2005, the changes to the locale settings do not appear in the FormatCurrency function.



CAUSE
The FormatCurrency function and other run-time functions that use the culture info strings do not call the Globalization.CultureInfo.CurrentCulture.ClearCachedData function. The Globalization.CultureInfo.CurrentCulture.ClearCachedData function is not called to avoid performance degradation.



WORKAROUND
To work around the problem, you can use either of the following methods:  You can call the ClearCachedData function before every call to the FormatCurrency function. To do this, follow these steps:   In step 3 of the &quot;More Information&quot; section, locate the following line of code: lReturn = SetLocaleInfo(1033, &H1C, CStr(intCount))   Add the following line of code after the code that you located in the previous step. Globalization.CultureInfo.CurrentCulture.ClearCachedData    You can change the locale settings of the current thread by using the managed culture info strings. The problem that is mentioned in the &quot;Symptoms&quot; section occurs when you use native Windows APIs to change the locale settings. Therefore, if you change the locale settings of the current thread by using the managed culture info strings, this problem may not occur.

To change the locale settings of the current thread in the managed code, replace the code in Module1 with the following code: Sub Main Dim Res As String Dim dblNum As Double = -1.1 Dim intCount As Integer Dim strExpect = {&quot;($1.1)&quot;, &quot;-$1.1&quot;, &quot;$-1.1&quot;, &quot;$1.1-&quot;, _ &quot;(1.1$)&quot;, &quot;-1.1$&quot;, &quot;1.1-$&quot;, &quot;1.1$-&quot;, _ &quot;-1.1 $&quot;, &quot;-$ 1.1&quot;, &quot;1.1 $-&quot;, &quot;$ 1.1-&quot;, _ &quot;$ -1.1&quot;, &quot;1.1- $&quot;, &quot;($ 1.1)&quot;, &quot;(1.1 $)&quot;}

Dim N As Globalization.NumberFormatInfo = Globalization.CultureInfo.CurrentCulture.NumberFormat.Clone Dim c As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture.Clone c.NumberFormat = N       Threading.Thread.CurrentThread.CurrentCulture = c

For intCount = 0 To 15 N.CurrencyNegativePattern = intCount Res = FormatCurrency(dblNum, 1, TriState.UseDefault, TriState.UseDefault) Console.WriteLine(&quot;Locale &quot; & Format(intCount, &quot;#00&quot;) & &quot; = &quot; & Res & vbTab & &quot;Expected = &quot; & strExpect(intCount)) Next Console.Write(&quot;Press any key to exit.&quot;) Console.Read End Sub 

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to reproduce the problem
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.</li> Create a new console application.</li>  Replace the code in Module1 with the following code:

Private Declare Function SetLocaleInfo Lib &quot;KERNEL32&quot; Alias &quot;SetLocaleInfoA&quot; (ByVal lcid As Integer, ByVal lctype As Integer, ByVal sBuf As String) As Integer Sub Main Dim Res As String Dim dblNum As Double = -1.1 Dim lReturn As Long Dim strInfo As String Dim intCount As Integer Dim strExpect = {&quot;($1.1)&quot;, &quot;-$1.1&quot;, &quot;$-1.1&quot;, &quot;$1.1-&quot;, _ &quot;(1.1$)&quot;, &quot;-1.1$&quot;, &quot;1.1-$&quot;, &quot;1.1$-&quot;, _ &quot;-1.1 $&quot;, &quot;-$ 1.1&quot;, &quot;1.1 $-&quot;, &quot;$ 1.1-&quot;, _ &quot;$ -1.1&quot;, &quot;1.1- $&quot;, &quot;($ 1.1)&quot;, &quot;(1.1 $)&quot;}

For intCount = 0 To 15 lReturn = SetLocaleInfo(1033, &H1C, CStr(intCount)) Res = FormatCurrency(dblNum, 1, TriState.UseDefault, TriState.UseDefault) Console.WriteLine(&quot;Locale &quot; & Format(intCount, &quot;#00&quot;) & &quot; = &quot; & Res & vbTab & &quot;Expected = &quot; & strExpect(intCount)) Next Console.Write(&quot;Press any key to exit.&quot;) Console.Read End Sub </li> On the Debug menu, click Start to run your application.

Notice that the changes to the locale settings do not appear in the FormatCurrency function in the Console window.</li></ol>

<div class="references_section">