Conversion

  • Thread starter Thread starter JJ
  • Start date Start date
J

JJ

Hello all,

Convert.ToDouble(this.comboBox2.Text.Substring(0,10));

This can result in an error, but sometimes, it doesn't, because
this.comboBox2.Text.Substring(0,10) is a value that CAN be converted to a
double. Is there a way to check for this possibility, and only convert when
the value CAN be converted?

Thanks in advance,

Vincent
 
JJ said:
Convert.ToDouble(this.comboBox2.Text.Substring(0,10));

This can result in an error, but sometimes, it doesn't, because
this.comboBox2.Text.Substring(0,10) is a value that CAN be converted to a
double. Is there a way to check for this possibility, and only convert when
the value CAN be converted?

The simplest way is just to use try/catch and catch the exception. You
could provide a hard-coded check to catch most of the error cases, but
you'll still want to use a try/catch for any that the simple test
doesn't reject but which still can't be converted (out of range etc).
 
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
You can try Double.TryParse( )

Doh - it shows how sleepy I am today that I'd forgotten double.TryParse
:(
 
Back
Top