I am getting a formatexception with the following code. Any one know how to make BooleanConverter convert from 0/1 to true/false.
bool bVal=true;
string sVal = "0";
Console.WriteLine(TypeDescriptor.GetConverter(bVal).ConvertFrom(sVal));
Thanks for the help!
If you’re going to use Int32.Parse, use Int32.TryParse instead. It doesn’t throw if the conversion fails, instead returning true or false. This means that it’s more performant if all you’re doing is checking to see if your input is a value. Example:
I have a tendency to go overboard on the ternary operator (x ? y : z), so here’s a slightly easier-to-read version:
(I tested them both. “1” returns true, “0” returns false.)