none
Cloning a culture RRS feed

  • Question

  • Hi,

    My observations on the IsReadOnly property of a CultureInfo object is as follows:

    1.      The default Thread.CurrentThread.CurrrentCulture is found to be read only

    2.      But if we explicitly set the Current thread’s culture to either of neutral, specific or invariant, and then if we check with the IsReadOnly method we find they are read/write.

    Imports System.Globalization
    Imports System.Threading
    
    Module Module1
    
        Sub Main()
           
            Console.WriteLine("Current Culture: {0}", Thread.CurrentThread.CurrentCulture.NativeName)
            Console.WriteLine("Read only property: {0}", Thread.CurrentThread.CurrentCulture.IsReadOnly)
    
            Thread.CurrentThread.CurrentCulture = New CultureInfo("da-dk")
            Console.WriteLine("Now we have set the current culture to {0}", Thread.CurrentThread.CurrentCulture.NativeName)
            Console.WriteLine("Read only property: {0}", Thread.CurrentThread.CurrentCulture.IsReadOnly)
    
            Thread.CurrentThread.CurrentCulture = New CultureInfo("da")
            Console.WriteLine("Now we have set the current culture to {0}", Thread.CurrentThread.CurrentCulture.NativeName)
            Console.WriteLine("Read only property: {0}", Thread.CurrentThread.CurrentCulture.IsReadOnly)
    
            Thread.CurrentThread.CurrentCulture = New CultureInfo("")
            Console.WriteLine("Now we have set the current culture to {0}", Thread.CurrentThread.CurrentCulture.NativeName)
            Console.WriteLine("Read only property: {0}", Thread.CurrentThread.CurrentCulture.IsReadOnly)
            Console.ReadLine()
    
        End Sub
    End Module
    

    Question:

    If we are explicitly setting the culture to neutral, specific, invariant we find the object of such a culture is of read/write nature. Then, is it right to understand that the Clone method is only  used to make a writable copy of the "read-only" default culture(i.e. the one we can set through control panel) Since all the other cases are already read/write?



    Sougata Ghosh

    Sunday, August 19, 2018 12:45 PM

Answers

  • Not necessarily.  There may be other reasons to clone a read/write culture instance.

    Perhaps you've changed the culture in code and made some modifications to some formatting that suit the majority of your application.  But then you add some new functionality that needs slightly different formatting and this functionality runs on a background thread.  So then you might clone the current custom culture, modify it, and then use that instance for this other thread.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by sougata12 Monday, August 20, 2018 4:47 AM
    Sunday, August 19, 2018 2:15 PM
    Moderator
  • Its just an explicit cast (ctype) versus an implicit cast.  In this case your code allows implicit casting and you know the Clone() method is always going to return the correct type so there is no discernable difference.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by sougata12 Monday, August 20, 2018 10:37 AM
    Monday, August 20, 2018 10:34 AM
    Moderator

All replies

  • Not necessarily.  There may be other reasons to clone a read/write culture instance.

    Perhaps you've changed the culture in code and made some modifications to some formatting that suit the majority of your application.  But then you add some new functionality that needs slightly different formatting and this functionality runs on a background thread.  So then you might clone the current custom culture, modify it, and then use that instance for this other thread.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by sougata12 Monday, August 20, 2018 4:47 AM
    Sunday, August 19, 2018 2:15 PM
    Moderator
  • Dim myCI As New CultureInfo("en-US", False)
          
          Dim myCIclone As CultureInfo = CType(myCI.Clone(), CultureInfo)

    Just a follow-up question:

    In many of the examples that are given in the MSDN documentation, I find that they have used "CType" as shown above. However if I do not use CType (i.e.if I write:

    Dim myCIclone As CultureInfo = myCI.Clone()

    then the code works fine. So is there any advantage of using "Ctype"?


    Sougata Ghosh

    Monday, August 20, 2018 6:29 AM
  • Its just an explicit cast (ctype) versus an implicit cast.  In this case your code allows implicit casting and you know the Clone() method is always going to return the correct type so there is no discernable difference.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by sougata12 Monday, August 20, 2018 10:37 AM
    Monday, August 20, 2018 10:34 AM
    Moderator