Home
»
Tutorials
»
String Conversion
String Conversion
I was doing some coding today and came across the need to convert a Y/N string to a boolean.
/// <summary>
///
Utility Class for String Conversions
/// </summary>
public static class
StringConversion
{
/// <summary>
///
Converts a Y or N string into a boolean.
/// </summary>
/// <param name="value">
Input value
</param>
/// <exception cref="ArgumentNullException">
An invalid null value was supplied.
</exception>
/// <exception cref="ArgumentOutOfRangeException">
A value other than y or n supplied.
</exception>
/// <returns>
bool
</returns>
public static bool
YNtoBool(
string
value)
{
if
(value ==
null
)
throw new
ArgumentNullException
(
"An invalid null value was supplied"
);
bool
valueIsYes = (
String
.Compare(value,
"y"
,
true
) == 0);
bool
valueIsNo = (
String
.Compare(value,
"n"
,
true
) == 0);
if
(!valueIsYes && !valueIsNo)
throw new
ArgumentOutOfRangeException
(
"value"
,
"A value other than y or n supplied"
);
return
(valueIsYes);
}
}
This article was published on the 8th March 2008
Last Edited 31st March 2008