-2

I cannot use GetCultures, from what I can tell it returns a blank list.

private void AddressChooser_Load(object sender, EventArgs e)
    {
        MessageBox.Show("Form load event successfully triggered") //Debug message - This appears at runtime
        foreach (string country in GetCountryList())
        {
            MessageBox.Show(country); //Debug message - This does not appear at runtime!!
            countryBox.Items.Clear();
            countryBox.Items.Add(country);
        }
    }

    public static List<string> GetCountryList()
    {
        MessageBox.Show("Function has been triggered successfully"); //Debug message - This appears at runtime
        List<string> cultureList = new List<string>();
        CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
        foreach (CultureInfo culture in cultures)
        {
            RegionInfo region = new RegionInfo(culture.LCID);
            if (!(cultureList.Contains(region.EnglishName)))
                cultureList.Add(region.EnglishName);
            MessageBox.Show(region.EnglishName); //Debug message - This does not appear at runtime!
        }
        return cultureList;
    }

I find it strange that this doesn't work considering it is simply a copy&pasted snippet. Please help! Thanks

YoshieMaster
  • 257
  • 3
  • 5
  • 13

3 Answers3

5

You must be sweeping an Exception under the floormat somewhere.

Your code fails because CultureTypes.AllCultures & ~CultureTypes.NeutralCultures doesn't work. Your list contains neutral cultures and new RegionInfo() throws.

The snippet:

var c1 = CultureTypes.AllCultures & ~CultureTypes.NeutralCultures;
Console.WriteLine(c1);

produces SpecificCultures, InstalledWin32Cultures and I suppose those Win32 cultures contain neutral ones.

The simple solution would be CultureInfo.GetCultures(CultureTypes.SpecificCultures);

But the main thing to fix would be your Exception handling and debugging techniques.

H H
  • 263,252
  • 30
  • 330
  • 514
  • I replaced CultureTypes.AllCultures & ~CultureTypes.NeutralCultures with CultureTypes.AllCultures and also tried with CultureTypes.SpecificCultures but no joy for either of them. – YoshieMaster Aug 03 '11 at 09:31
  • And did you see an Exception when using AllCultures? You should have. Better fix that first. – H H Aug 03 '11 at 09:36
  • Step through the code, maybe using .NeutralCultures. What happens when you create a `new Region()` ? – H H Aug 03 '11 at 10:56
  • Actually, I decided to just copy and paste a static list of countries into my ComboBoxes items list, so I don't have access to this code anymore. – YoshieMaster Aug 03 '11 at 10:59
  • Hmm, seems like bad practice to just leave this open, but none of the answers fixed my problem. What to do, what to do? – YoshieMaster Aug 03 '11 at 11:00
  • Again, I think you should worry about what happens to exceptions in your app. – H H Aug 03 '11 at 11:03
  • 1
    @YoshieMaster - So don't leave it open. Henk's answer is the correct answer. The fact it doesn't work for you means its a problem with YOUR program. – Security Hound Aug 03 '11 at 11:51
3

Firstly it is a good idea to Debug>>Exceptions and set the CLR Runtime Execptions to Thrown . Secondly I think this code can be what you are looking for:

        static void Main()
        {
            List<string> cultureList = new List<string>();
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
            foreach (CultureInfo culture in cultures)
            {   
                try
                {
                    RegionInfo region = new RegionInfo(culture.Name);
                    if (!(cultureList.Contains(region.EnglishName)))
                        cultureList.Add(region.EnglishName);
                    Console.WriteLine(region.EnglishName); 
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(String.Format("For{0} a specific culture name is required.", culture.Name));
                }                  
            }
        }
cgon
  • 1,955
  • 2
  • 23
  • 37
  • This doesn't return anything either (I changed your Console.WriteLines to MessageBox.Shows because it is a Windows Forms Application) – YoshieMaster Aug 03 '11 at 08:35
  • Huh, works in a console application. Weird. Just wont work in my Windows Forms Application. – YoshieMaster Aug 03 '11 at 08:38
  • By the way Henk Holterman solution CultureInfo.GetCultures(CultureTypes.SpecificCultures); is a good best practice and should be exepted as an answer. It is wise to value an effort. Thanks. – cgon Aug 03 '11 at 08:55
  • But it hasn't fixed my problem. – YoshieMaster Aug 03 '11 at 09:31
  • which one is your problem : your Winform application does not work or CultureInfo.GetCultures returns an empty list? If it is the first one it does not relate with your current question. If it is the second, you have the necessary answers here I think... – cgon Aug 03 '11 at 10:51
  • The CultureInfo.GetCultures returns an empty list still, even after trying all the solutions here. – YoshieMaster Aug 03 '11 at 10:54
  • @caglar_gonul let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2091/discussion-between-yoshiemaster-and-caglar-gonul) – YoshieMaster Aug 03 '11 at 10:57
-1

Use a bitwise or instead of and here CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68