c# - IF Boolean statement order of input validation -
i have validate inputs user makes, , send error message
this got far
// probes methods check validity. private void btncalculate_click(object sender, eventargs e) { if (!(validwidth(float.parse(txtwidth.text)))) { return; } if (!(validlength(float.parse(txtlength.text)))) { return; } if (!(validdepth(float.parse(txtavgdepth.text)))) { return; } } my problem when enter values length, width, , depth. in order..what mean if don't enter width , leave blank , put in length , depth gives me unhandled expection.
here methods
/** created boolean method test if written width valid or not valid **/ private bool validwidth(float width1) { float width = float.parse(txtwidth.text); { if (width >= 2 & width <= 20) { return true; } else { string title = "data invalid"; string msg = "width measurement invalid \n place enter value between 2 , 20"; dialogresult response; response = messagebox.show(msg, title, messageboxbuttons.ok, messageboxicon.exclamation); return false; } } } /** created boolean method test if written legnth valid or not valid **/ private bool validlength(float length1) { float length = float.parse(txtlength.text); { if (length >= 5 & length <= 50) { return true; } else { string title = "data invalid"; string msg = "legnth measurement invalid \n place enter value between 5 , 50"; dialogresult response; response = messagebox.show(msg, title, messageboxbuttons.ok, messageboxicon.exclamation); return false; } } } /** created boolean method test if written legnth valid or not valid **/ private bool validdepth(float depth1) { float depth = float.parse(txtavgdepth.text); if (depth >= 2 & depth <= 4) { return true; } else { string title = "data invalid"; string msg = "average depth measurement invalid \n place enter value between 2 , 4"; dialogresult response; response = messagebox.show(msg, title, messageboxbuttons.ok, messageboxicon.exclamation); return false; } }
the parse method throw exception if feed empty string. should catch exception, or use tryparse.
Comments
Post a Comment