Addendum: Most of the time you won't have this great of an error rate (50%). But, you only need about 1 in a 1000 to have a parse error to make TryParse faster than a straight Parse/TryCatch.
If you are 100% sure that there are going to be no parse errors, a straight Parse is about 80% faster.
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
Fantastic! Great snippet!
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
if (the value being passed == to code within the application)
{
this.methodGreat;
}
else
{
this.methodSucketh;
}
private double d;
public double d2 {get{return d;}}
What if the value being entered in a textbox has to be an double value?
try
{
d = double.Parse(textBox1.text);
this.DialgogResult = DialogResult.Ok;
}
catch
{
this.DialgoResult = DialogResult.Cancel;
}
if you want to show error message to the user Exception can be a plus because it's an exception doesn't always make using the exception bad like this.
try
{
d = double.Parse(textBox1.text);
}
catch(Exception ex)
{
frmMessage msg = new frmMessage(ex);
msg.Title = ex.Message;
msg.ShowDialog();
}
now we can use the exception that we are passing to the frmMessage form to show more datailed information on the exception that occurred;
or we can use a
Console.WriteLine("-------Error Occurred In Application-------");
Console.WriteLine("The Error Occured On " + DateTime.Now.ToLongDateString() + "At " + DateTime.Now.ToShortTimeString());
Console.WriteLine("Application Experienced Error: " + ex.Message);
Console.WriteLine("Would You Like To Retry The Operation? y/n")
string retry = Console.ReadLine().ToLower();
if (retry == "y")
{Application.DoEvents();}
CsharpChico
Junior Poster in Training
72 posts since May 2010
Reputation Points: 12
Solved Threads: 8