Possible to Overloading int.parse

  • Thread starter Thread starter Julia
  • Start date Start date
J

Julia

Hi,
I need a safe parse function which get a default value if the parse was
failed
can i Overload int.parse


Thanks.
 
Hi Julia,

Putting it inside a try/catch block will allow you to set another value if
the parse fails (causes an exception).

int n = 0;
try
{
n = Int32.Parse(string);
}
catch
{
n = -1;
}

The default value in this case would be -1;

You could also use Double.TryParse which returns false if the parse
failed, although the value range acceptable would be for double.
 
Back
Top