mysql - C# Syntax issue in my If statment -
im having issues if statment , 1 in think main issue in:
(string.isnullorwhitespace(txtfirstname.text) || string.isnullorwhitespace(txtlastname.text) || string.isnullorwhitespace(txtemail.text) || string.isnullorwhitespace(txthotel.text))
i want if statements false should execute mysql command (ps working moment syntax fells wrong.)
if (appointment.checkstate == checkstate.checked) { if (string.isnullorwhitespace(txtfirstname.text)) { txtfirstname.backcolor = color.red; messagebox.show("please enter first name!"); } else { txtfirstname.backcolor = color.white; } if (string.isnullorwhitespace(txtlastname.text)) { txtlastname.backcolor = color.red; messagebox.show("please enter last name!"); } else { txtlastname.backcolor = color.white; } if (string.isnullorwhitespace(txtemail.text)) { txtemail.backcolor = color.red; messagebox.show("please enter email!"); } else { txtemail.backcolor = color.white; } if (string.isnullorwhitespace(txthotel.text)) { txthotel.backcolor = color.red; messagebox.show("please enter valid hotel!"); } else { txthotel.backcolor = color.white; } if (string.isnullorwhitespace(txtfirstname.text) || string.isnullorwhitespace(txtlastname.text) || string.isnullorwhitespace(txtemail.text) || string.isnullorwhitespace(txthotel.text)) { } else { ///register client connect.open(); mysqlcommand command = new mysqlcommand("insert client (firstname,lastname,nationality,mobile,email,budget,comments) value(@firstname,@lastname,@nationality,@mobile,@email,@budget,@comments)", connect); command.parameters.addwithvalue("@firstname", txtfirstname.text); command.parameters.addwithvalue("@lastname", txtlastname.text); command.parameters.addwithvalue("@nationality", txtnationality.text); command.parameters.addwithvalue("@mobile", txtmobile.text); command.parameters.addwithvalue("@email", txtemail.text); command.parameters.addwithvalue("@budget", int.parse(txtbudget.text)); command.parameters.addwithvalue("@comments", txtcomments.text); command.executenonquery(); connect.close(); loadclient(); ///register appointment connect.open(); command = new mysqlcommand("insert appointment(hotel,roomnumber,appointmentdate,appointmenttime,confirmby,propertytype,bedrooms,purpose,interestedin,departuredate) value(@hotel,@roomnumber,@appointmentdate,@appointmenttime,@confirmby,@propertytype,@bedrooms,@purpose,@interestedin,@departuredate)", connect); command.parameters.addwithvalue("@hotel", txthotel.text); command.parameters.addwithvalue("@roomnumber", int.parse(txtroomnumber.text)); command.parameters.addwithvalue("@appointmentdate", datetimepicker2.value.date); command.parameters.addwithvalue("@appointmenttime", cmbtimeapp.text); command.parameters.addwithvalue("@confirmby", cmbconfirm.text); command.parameters.addwithvalue("@propertytype", cmbpropertytype.text); command.parameters.addwithvalue("@bedrooms", cmbbedroom.text); command.parameters.addwithvalue("@purpose", cmbpurpose.text); command.parameters.addwithvalue("@interestedin", cmbintrestedin.text); command.parameters.addwithvalue("@departuredate", datetimepicker3.value.date); command.executenonquery(); connect.close(); messagebox.show("appointment registered!"); } }
obviously logic expression of type:
if (condition1 || condition2 || condition3 || ....) { doa(); } else { dob(); }
can transformed to:
if (!condition1 && !condition2 && !condition3 && ....) { dob(); } else { doa(); }
this solve problem code readability still suspect
if (!string.isnullorwhitespace(txtfirstname.text) && !string.isnullorwhitespace(txtlastname.text) && !string.isnullorwhitespace(txtemail.text) && !string.isnullorwhitespace(txthotel.text)) { //... }
yuck!
how can improve this? using auxiliary local variables makes code easier read; highlights semantics of code , hides mechanics. locals extremely cheap (or free), use them!
consider following approach:
var isvalidfirstname = !string.isnullorwhitespace(txtfirstname.text) var isvalidlastname = !string.isnullorwhitespace(txtlastname.text) var isvalidemail = !string.isnullorwhitespace(txtemail.text) var isvalidhotel = !string.isnullorwhitespace(txthotel.text))
and if
statement looks this:
if (isvalidfirstname && isvalidlastname && isvalidemail && isvalidhotel) { //... }
doesn't read better?
but why stop here? can't abstract whole guest info validation away? sure can:
public static bool isvalidguestinfo(string firstname, string lastname, string email, string hotel) { var isvalidfirstname = !string.isnullorwhitespace(txtfirstname.text) var isvalidlastname = !string.isnullorwhitespace(txtlastname.text) var isvalidemail = !string.isnullorwhitespace(txtemail.text) var isvalidhotel = !string.isnullorwhitespace(txthotel.text)) return isvalidfirstname && isvalidlastname && isvalidemail && isvalidhotel; }
and if
statement simply:
if (isvalidguestinfo(firstname, lastname, email, hotel)) { //... }
now reads better, semantics clear , mechanisms used not in way. also, boot, if need validate guest info anywhere else in code not need duplicate code.
Comments
Post a Comment