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)

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:
    1. In step 3 of the "More Information" section, locate the following line of code:
      	lReturn = SetLocaleInfo(1033, &H1C, CStr(intCount))
    2. 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.

MORE INFORMATION

Steps to reproduce the problem

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. Create a new console application.
  3. Replace the code in Module1 with the following code:
        Private Declare Function SetLocaleInfo Lib "KERNEL32" Alias "SetLocaleInfoA" (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() = {"($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 $)"}
    
            For intCount = 0 To 15
                lReturn = SetLocaleInfo(1033, &H1C, CStr(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
  4. 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.

REFERENCES

For additional information about the FormatCurrency function, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbformat kbConsole kbprb KB834064 kbAudDeveloper