我的if语句存在问题,希望有人看到它,我认为我的主要问题在于:
(string.IsNullOrWhiteSpace(txtFirstName.Text) || string.IsNullOrWhiteSpace(txtLastName.Text) || string.IsNullOrWhiteSpace(txtEmail.Text) || string.IsNullOrWhiteSpace(txthotel.Text))
我想如果所有语句都是假的,它应该执行mysql命令(Ps它暂时正在工作,但语法错误。)
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 a 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 into 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 into 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!");
}
}
答案 0 :(得分:3)
显然任何类型的逻辑表达式:
if (Condition1 || Condition2 || Condition3 || ....)
{
DoA();
}
else
{
DoB();
}
可以转换为:
if (!Condition1 && !Condition2 && !Condition3 && ....)
{
DoB();
}
else
{
DoA();
}
这可以解决您的问题,但代码可读性仍然可疑
if (!string.IsNullOrWhiteSpace(txtFirstName.Text) &&
!string.IsNullOrWhiteSpace(txtLastName.Text) &&
!string.IsNullOrWhiteSpace(txtEmail.Text) &&
!string.IsNullOrWhiteSpace(txthotel.Text))
{
//...
}
呸!
我们如何改善这一点?有时简单地使用辅助局部变量会使代码更容易阅读;它突出了代码的语义并隐藏了机制。当地人非常便宜(甚至免费),使用它们!
考虑以下方法:
var isValidFirstName = !string.IsNullOrWhiteSpace(txtFirstName.Text)
var isValidLastName = !string.IsNullOrWhiteSpace(txtLastName.Text)
var isValidEmail = !string.IsNullOrWhiteSpace(txtEmail.Text)
var isValidHotel = !string.IsNullOrWhiteSpace(txthotel.Text))
现在您的if
语句如下所示:
if (isValidFirstName &&
isValidLastName &&
isValidEmail &&
isValidHotel)
{
//...
}
那不是读得好多了吗?
但为何停在这里?我们不能只抽象整个客户信息验证吗?当然,我们可以:
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;
}
现在你的if
声明只是:
if (IsValidGuestInfo(firstName, lastName, email, hotel))
{
//...
}
现在读得更好,语义清晰并且使用的机制不会妨碍。此外,要启动,如果您需要在代码中的任何其他位置验证访客信息,则无需重复代码。
答案 1 :(得分:1)
我会将其全部放入if
并放弃else
部分
if (!string.IsNullOrWhiteSpace(txtFirstName.Text) && !string.IsNullOrWhiteSpace(txtLastName.Text) && !string.IsNullOrWhiteSpace(txtEmail.Text) && !string.IsNullOrWhiteSpace(txthotel.Text))
{
//register client
connect.Open();
...
答案 2 :(得分:0)
基本上,这个
(string.IsNullOrWhiteSpace(txtFirstName.Text) || string.IsNullOrWhiteSpace(txtLastName.Text) || string.IsNullOrWhiteSpace(txtEmail.Text) || string.IsNullOrWhiteSpace(txthotel.Text))
如果任何操作数为true,则为true。你把它放到一个空的if和else处理命令。这是正确的,但它过于复杂。你可以这样检查
if (!(string.IsNullOrWhiteSpace(txtFirstName.Text) || string.IsNullOrWhiteSpace(txtLastName.Text) || string.IsNullOrWhiteSpace(txtEmail.Text) || string.IsNullOrWhiteSpace(txthotel.Text)))
{
///register client
connect.Open();
MySqlCommand command = new MySqlCommand("Insert into 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 into 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!");
}
答案 3 :(得分:0)
Lajos'答案很美。 我可能想以相反的方式尝试:
if(string.IsNullOrWhiteSpace(txtFirstName.Text) || string.IsNullOrWhiteSpace(txtLastName.Text) || string.IsNullOrWhiteSpace(txtEmail.Text) || string.IsNullOrWhiteSpace(txthotel.Text))
{
return; //Isn't it short and simple?
//Or if you want to jump somewhere and not to exit....
goto dosomething:
}
dosomething:
MessageBox.Show("Something is blank! ");