[英]String does contain a definition for isnullorwhitespace
請幫助我。 我不知道isnullorwhitespace
什么問題:
我的錯誤是:
“字符串確實包含對isullorwhitespace的定義”
我對isnullorwhitespace
其他選擇是isnullorwhitespace
?
private void btnAdd_Click(object sender, EventArgs e)
{
int age = int.Parse(txtage.Text);
if (string.IsNullOrWhiteSpace(txtfname.Text))
{
MessageBox.Show("Please input your firstname!");
}
else if(string.IsNullOrWhiteSpace(txtlname.Text))
{
MessageBox.Show("Please input your lastname!");
}
else if(string.IsNullOrWhiteSpace(cbgender.SelectedItem.ToString()))
{
MessageBox.Show("Please specify your gender!");
}
else if(string.IsNullOrWhiteSpace(txtage.Text) || age <= 0 )
{
MessageBox.Show("Please assign your birthdate to know your age!");
}
else if(string.IsNullOrWhiteSpace(txtcontact.Text))
{
MessageBox.Show("Please input your contact number!");
}
else if(string.IsNullOrWhiteSpace(txtAddress.Text))
{
MessageBox.Show("Please input your address!");
}
else if(string.IsNullOrWhiteSpace(txtUsername.Text))
{
MessageBox.Show("Please input your username!");
}
else if(string.IsNullOrWhiteSpace(txtPass.Text))
{
MessageBox.Show("Please input your password!");
}
else if(string.IsNullOrWhiteSpace(cbxQuest.SelectedItem.ToString()))
{
MessageBox.Show("Please specify your secret question!");
}
else if(string.IsNullOrWhiteSpace(txtAsnwer.Text))
{
MessageBox.Show("Please input your answer!");
}
else
{
//Insert 7
ui.fname = txtfname.Text;
ui.lname = txtlname.Text;
ui.gender = cbgender.SelectedItem.ToString();
ui.age = int.Parse(txtage.Text);
ui.bdate = dtpBdate.Value;
ui.contactNo = txtcontact.Text;
ui.Adress = txtAddress.Text;
//Insert 4
us.username = txtUsername.Text;
us.passwordHash = txtPass.Text;
us.secretQuestion = cbxQuest.SelectedItem.ToString();
us.secretAnswer = txtAsnwer.Text;
StoredProcedure.Insert(ui, us);
dgData.DataSource = StoredProcedure.View();
Clear();
}
.NET 4中引入了String.IsNullOrWhiteSpace。如果您不針對.NET 4,則可以輕松編寫自己的代碼:
public static class StringExtensions
{
public static bool IsNullOrWhiteSpace(string value)
{
if (value != null)
{
for (int i = 0; i < value.Length; i++)
{
if (!char.IsWhiteSpace(value[i]))
{
return false;
}
}
}
return true;
}
}
可以這樣使用:
if (StringExtensions.IsNullOrWhiteSpace(txtfname.Text))
{
// do something
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.