-2

How can I convert String to CultureInfo?

Cultureinfo ci;
String dummy = dropdownlist.SelectedValue; 
ci = (CultureInfo) dummy; // <- compile time error here

Error: cannot convert string to CultureInfo.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Abdul Rahman
  • 41
  • 2
  • 8
  • 3
    First problem: C# is case-sensitive. There's no such type as `cultureinfo`, and I suspect you mean `SelectedValue` too. Please make your code as close to reality as possible. Second problem: you can't convert between types arbitrarily like this. Third problem: we don't even know what values you're trying to convert. – Jon Skeet Sep 22 '15 at 13:02

2 Answers2

1

You an use CultureInfo.GetCultureInfo, for example:

System.Globalization.CultureInfo.GetCultureInfo("en");
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
1

Well, just a straightforward creation:

 CultureInfo ci = new CultureInfo(dummy);

The entire solution:

 CultureInfo ci = String.IsNullOrEmpty(dropdownlist.SelectedValue)
   ? CultureInfo.InvariantCulture // Or use other default
   : new CultureInfo(dropdownlist.SelectedValue);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215