c# - How to implement validation for negative values -
i working on code snippet
private void calculatebutton_click(object sender, eventargs e) { int amount; if (int.tryparse(amounttextbox.text, out amount)) { wantedtextbox.text = currency_exchange.exchangecurrency((currencies)currencycombobox.selectedindex, (currencies)wantedcurrencycombobox.selectedindex, amount).tostring("0.00"); wantedcurrencylable.text = ((currencies)wantedcurrencycombobox.selectedindex).tostring(); } else { messagebox.show("invalid amount"); } }
and realized way late should put in validation negative numbers. way i've set code makes difficult. how this?
store value of tryparse
result, check amount
in same if
statement, so:
boolean parseresult = int.tryparse(amounttextbox.text, out amount) if (parseresult && amount >= 0) { //.... } else { messagebox.show("invalid amount"); }
Comments
Post a Comment