Considering s
is a string value, you can't cast it to int
. If int.TryParse
is not available then you can create your own method which would return a bool . Something like:
public static class MyIntConversion
{
public static bool MyTryParse(object parameter, out int value)
{
value = 0;
try
{
value = Convert.ToInt32(parameter);
return true;
}
catch
{
return false;
}
}
}
and then to use it:
int temp;
if (!MyIntConversion.MyTryParse("123", out temp))
{
MessageBox.Show("That was not an int! Consider this a lint-like hint!");
}
int.TryParse
internally uses try-catch
to do parsing and is implemented in similar manner.