4

I am trying to run this exact line, and it isn't working. Anyone know the reason why?

Convert.ToBoolean("verdadero", new System.Globalization.CultureInfo("ES-MX"));

I am parsing this from an xml file generated by a program that has many languages installed, and so it will use "true" in "EN-US" culture or "verdadero" in "ES-MX".

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Bimsley
  • 73
  • 1
  • 7
  • 1
    Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Jul 11 '13 at 19:55
  • Does the XML document have an XML schema (e.g, XSD file) or other specification? If so, it should enumerate all the possible values. – Tom Blodget Jul 11 '13 at 23:59

1 Answers1

3

Interesting. Running Convert.ToBoolean through a decompiler emits this:

/// <summary>
/// Converts the specified string representation of a logical value to its Boolean equivalent, using the specified culture-specific formatting information.
/// </summary>
/// 
/// <returns>
/// true if <paramref name="value"/> equals <see cref="F:System.Boolean.TrueString"/>, or false if <paramref name="value"/> equals <see cref="F:System.Boolean.FalseString"/> or null.
/// </returns>
/// <param name="value">A string that contains the value of either <see cref="F:System.Boolean.TrueString"/> or <see cref="F:System.Boolean.FalseString"/>. </param><param name="provider">An object that supplies culture-specific formatting information. This parameter is ignored.</param><exception cref="T:System.FormatException"><paramref name="value"/> is not equal to <see cref="F:System.Boolean.TrueString"/> or <see cref="F:System.Boolean.FalseString"/>. </exception><filterpriority>1</filterpriority>
[__DynamicallyInvokable]
public static bool ToBoolean(string value, IFormatProvider provider)
{
  if (value == null)
    return false;
  else
    return bool.Parse(value);
}

This makes it look like the IFormatProvider is completely disregarded.

I'm tempted to say it's a bug in the framework, but experience has taught me that I'm usually missing something when I come to that conclusion...

Rob H
  • 1,840
  • 16
  • 25
  • Unfortunately, I'm not seeing any good options. http://stackoverflow.com/questions/491334/why-does-boolean-tostring-output-true-and-not-true – Rob H Jul 11 '13 at 20:11