'Convert string "True" and "False" to 1 and 0 [duplicate]
I'm retrieving a string value of "True" or "False". I need to convert it to 1 or 0 respectively. What's the best way to do that? Currently I'm doing it this way....
var myValue = Convert.ToInt16(Convert.ToBoolean(stringValue));
If I remove the ToBoolean part....
var myValue = Convert.ToInt16(stringValue);
.... then I get "Input string was not in a correct format" for the ToInt16 conversion. Am I doing this in the best way?
Solution 1:[1]
simple if/else approach
short result = stringValue == "True" ? (short)1 : (short)0;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fubo |
