以下是我收到错误的代码:
此代码用于Windows窗体,以验证用户是否在文本框中输入信息
private void btn_Submit_Click(object sender, EventArgs e)
{
if(validateform())
{
return;
}
}
private bool validateform()
{
if (string.IsNullOrWhiteSpace(txtCustomerID.Text))
{
MessageBox.Show("Customer ID is required.");
return false;
}
if (string.IsNullOrWhiteSpace(txtProductID.Text))
{
MessageBox.Show("Product ID is required.");
return false;
}
if (string.IsNullOrWhiteSpace(txtOrderQty.Text))
{
MessageBox.Show("Order Qty is required.");
return false;
}
if (string.IsNullOrWhiteSpace(txtUnitPrice.Text))
{
MessageBox.Show("Unit Price is required.");
return false;
}
if (string.IsNullOrWhiteSpace(txtSubTotal.Text))
{
MessageBox.Show("Sub Total is required.");
return false;
}
if (string.IsNullOrWhiteSpace(txtTaxAmount.Text))
{
MessageBox.Show("Tax Amount is required.");
return false;
}
if (string.IsNullOrWhiteSpace(txtTotalDue.Text))
{
MessageBox.Show("Total Due is required.");
return false;
}
return true; <<<Return is underlined and the error reads: Invalid token 'return' in class, struct, or interface member declaration
}
答案 0 :(得分:0)
您在上一个if语句后缺少一个开括号。这意味着右括号实际上是在关闭类,因此你的最后一个return语句在类之外。
只需添加左括号:
// Other code here
if (string.IsNullOrWhiteSpace(txtTotalDue.Text))
{
MessageBox.Show("Total Due is required.");
return false;
}
return true;
}