What's the best way to do the equivalent of int.TryParse (which is found in .net 2.0 onwards) using .net 1.1.
The Parse method returns the converted number; the TryParse method returns a boolean value that indicates whether the conversion succeeded, and returns the converted number in an out parameter. If the string isn't in a valid format, Parse throws an exception, but TryParse returns false .
TryParse method returns false i.e. a Boolean value, whereas int. Parse returns an exception.
TryParse(String, NumberStyles, IFormatProvider, Int32) Converts the string representation of a number in a specified style and culture-specific format to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded.
Obviously,
class Int32Util
{
public static bool TryParse(string value, out int result)
{
result = 0;
try
{
result = Int32.Parse(value);
return true;
}
catch(FormatException)
{
return false;
}
catch(OverflowException)
{
return false;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With