gpt4 book ai didi

我的 If 语句中的 C# 语法问题

转载 作者:行者123 更新时间:2023-11-29 04:37:46 25 4
gpt4 key购买 nike

我的 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!");
}



}

最佳答案

显然是该类型的任何逻辑表达式:

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))
{
//...
}

现在读起来更好了,语义清晰了,而且使用的机制也没有阻碍。此外,启动时,如果您需要在代码中的其他任何地方验证 guest 信息,则无需复制代码。

关于我的 If 语句中的 C# 语法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37113283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com