Last post Sep 07, 2010 09:22 PM by sansan
Sep 07, 2010 09:13 PM|SweeKian|LINK
The scenario is this:
I have a .aspx webform, with code behind to handle data entered by user, most fields are in double.
At business layer, at other class, let say there is a function:
Double value1 = 0.0
Double value2 = 0.0
double.TryParse(txtDouble.txt, out value1)
double.TryParse(txtDouble2.txt, out value2)
I just wondering whether this is the correct way of writing it? And if I have more than 20 fields of double value, then I should declare additional 20 double value?
Your input is much appreciated.
Sep 07, 2010 09:20 PM|mbanavige|LINK
you should definately explicitly convert the user input which is string data into the decimal type that you need before trying to process it.
Since you're posting to the vb.net section, the vb code would look like this:
Dim value1 As Double
Dim value2 As Double
note that the TryParse methods return False if the parse fails, so you could opt not to save the data if it looks like it might be invalid.
Sep 07, 2010 09:22 PM|sansan|LINK
if I have more than 20 fields of double value, then I should declare additional 20 double value?
You can conditionally check for empty strings and pass zero as the parameter value
SaveData(String.IsNullOrEmpty(txtDouble1.Text) ? 0 : Double.Parse(txtDouble1.Text));
With this code, I'm assuming that you have compare validators for the texboxes to validate the Double value entered in the text so that valid values can only be parsed.