.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Wrong NumberGroupSeparator at String.Format() with German ACADM 2015

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
gerundt
1178 Views, 5 Replies

Wrong NumberGroupSeparator at String.Format() with German ACADM 2015

I have a AutoCAD-AddIn which run in the past with ACADM 2013 and is now compiled for ACADM 2015. Unfortunately I have now problems with the number group separator. The following VB source code shows “1,234 Suchergebnisse” (but we use “.” instead “,” in Germany):

 

Dim iCount As Integer = 1234
Debug.Print("{0:N0} Suchergebnisse", iCount)

 The same source code works under ACADM 2013 without problems! And I have NO problems in other programs (for example Excel).

 

I know meanwhile that “Globalization.CultureInfo.CurrentCulture” is set correct to German, but the number group separator (“Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator”) use the wrong char.

 

If I set the NumberGroupSeparator manually to “.” at the initialization from the AddIn the code works like expected:

 

Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator = "."

Is this a problem for AutoCAD? Or is there a option, where I can set the separator in AutoCAD?

 

Thanks for your help!

 

Greetings,
Tim Gerundt

5 REPLIES 5
Message 2 of 6
gerundt
in reply to: gerundt

As example source code you must use the following:

 

Dim iCount As Integer = 1234

Debug.Print(String.Format("{0:N2} Suchergebnisse", iCount))

 

It seems, that Debug.Print("{0:N0} Suchergebnisse", iCount) never use the CurrentCulture.

 

I tested my problem with a windows form, so I don't notice the bug in the example code. 😉

 

But the problem remain, it was only a typo in my example code!

Message 3 of 6
Balaji_Ram
in reply to: gerundt

Hello,

 

Yes, AutoCAD does reset the number group separator.

 

http://adndevblog.typepad.com/autocad/2012/06/autocad-keeps-overriding-currentculture-and-currentuic...

 

By specifying a NumberFormat to use with the "ToString" method, you can format it differently

 

System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("de-DE");
ed.WriteMessage(String.Format("{0}{1}", Environment.NewLine, iCount.ToString("N", ci.NumberFormat)));

 

Regards,

Balaji

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 4 of 6
gerundt
in reply to: Balaji_Ram

Hello Balaji,

 

thank for your answer and the link!

 

Change every String.Format() and .ToString() functions in the large source code base is no option for me. Specially if you know, that it worked with ACADM 2013 without change.

 

I currently test the follwing code and hope, that it don't break AutoCAD:

 

Public Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize
    Dim oGermanCulture As New Globalization.CultureInfo("de-DE")

    Threading.Thread.CurrentThread.CurrentCulture = oGermanCulture
    Threading.Thread.CurrentThread.CurrentUICulture = oGermanCulture
End Sub

 

Greetings,

Tim

Message 5 of 6
Balaji_Ram
in reply to: gerundt

Hello Tim,

 

Yes, including the formatting change in all your code can be quite a task.

 

As mentioned in that blog post, it is possible that AutoCAD can reset the settings to what it was.

 

Unfortunately, it will not be possible for me to confirm if that will happen.

 

Using the NumberFormat with ToString is a sure way of formatting it as per your format.

 

Regards,

Balaji

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 6 of 6
gerundt
in reply to: Balaji_Ram

I had some more time and ported the AutoCAD DevBlog code to VB.net:

 

Imports System.Globalization
Imports System.Threading

''' <summary>
''' ...
''' </summary>
''' <remarks>http://adndevblog.typepad.com/autocad/2012/06/autocad-keeps-overriding-currentculture-and-currentuic...
Public Class MyCultureOverride

    Private _PrevCulture As CultureInfo
    Private _PrevUICulture As CultureInfo

    Friend Shared ReadOnly CultureOverride As CultureInfo = CultureInfo.CreateSpecificCulture("de-DE")

#Region "New"
    ''' <summary>
    ''' ...
    ''' </summary>
    Private Sub New()
        If CultureOverride IsNot Nothing Then
            _PrevCulture = Thread.CurrentThread.CurrentCulture
            _PrevUICulture = Thread.CurrentThread.CurrentUICulture

            Thread.CurrentThread.CurrentCulture = CultureOverride
            Thread.CurrentThread.CurrentUICulture = CultureOverride
        End If
    End Sub
#End Region

    ''' <summary>
    ''' ...
    ''' </summary>
    Private Sub Restore()
        If _PrevCulture IsNot Nothing AndAlso _PrevUICulture IsNot Nothing Then
            Thread.CurrentThread.CurrentCulture = _PrevCulture
            Thread.CurrentThread.CurrentUICulture = _PrevUICulture
        End If
    End Sub

    ''' <summary>
    ''' ...
    ''' </summary>
    ''' <param name="useAction">Aktion</param>
    Public Shared Sub Use(useAction As Action)
        If useAction Is Nothing Then
            Throw New ArgumentNullException("useAction")
        End If

        Dim oCultureOverride = New MyCultureOverride()

        Try
            useAction()
        Finally
            oCultureOverride.Restore()
        End Try
    End Sub

End Class

 

You can test it with the following code:

 

<CommandMethod("MYTEST1")> _
Public Sub Test1Command()
    Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(String.Format("WITHOUT MyCultureOverride = {0:N2}", 1234))
End Sub

<CommandMethod("MYTEST2")> _
Public Sub Test2Command()
    MyCultureOverride.Use(AddressOf Test2CommandAction)
End Sub

Private Sub Test2CommandAction()
    Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(String.Format("With MyCultureOverride = {0:N2}", 1234))
End Sub

 

 I hope it is the correct way and I need less changes at the code instead switch all numbers to ToString(own culture).

 

Thanks for your help! 🙂

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost