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 (834064)
The information in this article applies to:
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
SYMPTOMSWhen 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.CAUSEThe 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.WORKAROUNDTo 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 "More Information" 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 "Symptoms" 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() = {"($1.1)", "-$1.1", "$-1.1", "$1.1-", _
"(1.1$)", "-1.1$", "1.1-$", "1.1$-", _
"-1.1 $", "-$ 1.1", "1.1 $-", "$ 1.1-", _
"$ -1.1", "1.1- $", "($ 1.1)", "(1.1 $)"}
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("Locale " & Format(intCount, "#00") & " = " & Res & vbTab & "Expected = " & strExpect(intCount))
Next
Console.Write("Press any key to exit.")
Console.Read()
End Sub
STATUS This
behavior is by design.REFERENCESFor additional information about the FormatCurrency function, visit the following Microsoft Developer Network (MSDN)
Web site:
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbformat kbConsole kbprb KB834064 kbAudDeveloper |
---|
|