In my application, I have a text field - txtDiscount , where the administrator can set the discount percentage for a specific user, or he may not be. At the back end, I want to save the data, so now I have this:
double? discount = null; if (!string.IsNullOrEmpty(txtDiscount.Text)) { if (!double.TryParse(txtDiscount.Text, out discount)) errors.Add("Discount must be a double."); }
So, I get an error message for invalid argument and obviously this is a discount , which cannot be NULL if I am going to use it in TryParse . I saw that many people make extensions for such situations, but so far I do not think it is necessary. What I can think of is to use another variable:
double? discount = null; private double _discount; if (!string.IsNullOrEmpty(txtDiscount.Text)) { if (!double.TryParse(txtDiscount.Text, out _discount)) { errors.Add("Discount must be adouble."); } else { discount = _discount; } }
and then use my nullable discount to pass the value to the database. But in fact, I do not like the code above, it seems to me that it is quite difficult for such a task, but I can not come up with something better. So, how can I handle this situation without using the extension method?
source share