...
Why this don't work:
Boolean test;
String testValue;
testValue="0";
test=System.Convert.ToBoolean(testValue);
How can I convert string to boolean?
By using the string equivalents of the boolean values:
"True" or "False".
This works, but I need to use 2 conversions:
test=Convert.ToBoolean(Convert.ToInt16(testValue));
Can this be done with single conversion?
Nope.
The question should rather be:
Why do you use a string to contain a
numeric value in the first place?
A Boolean can only have one of two values: true or false
If you want to use a string to contain a boolean value, you could use the
values I mentioned above.
You can also convert from a numeric value to a boolean, but another question
should be:
Why does the conversion from a numeric value
to a Boolean work at all?
To simplify wrapping of legacy code in C/C++ where boolean values actually
*were* numeric values (0=false, 1=true), but there were no direct
conversions from strings to boolean values in those languages either.
So, as a conclusion, these two alternatives gives the "single-conversions":
String testValue = "False";
Boolean test = System.Convert.ToBoolean(testValue);
---------------------------------------------------
int testValue = 0;
Boolean test = System.Convert.ToBoolean(testValue);
// Bjorn A