我为Windows窗体应用程序编写了此代码。这是单击按钮的事件处理程序之一。
private void btnLP_Click(object sender, EventArgs e)
{
bool isValidData()
{
return
IsPresent(txtLoanAmountLP) &&
IsPresent(txtInterstLP) &&
IsPresent(txtYearsLP) &&
IsDecimal(txtLoanAmountLP) &&
IsDecimal(txtInterstLP) &&
IsDecimal(txtYearsLP) &&
IsWithinRange(txtLoanAmountLP, 1, 999999) &&
IsWithinRange(txtInterstLP, 1, 18) &&
IsWithinRange(txtYearsLP, 1, 30);
}
try
{
if (isValidData())
{
double Loan = Convert.ToDouble(txtLoanAmountLP.Text);
double IR = Convert.ToDouble(txtInterstLP.Text);
int yrs = Convert.ToInt32(txtYearsLP.Text);
double MP = CalcLP(Loan, IR, yrs);
txtMonthlyPaymentLP.Text = MP.ToString("c");
txtLoanAmountLP.SelectAll();
txtLoanAmountLP.Focus();
totalCal++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" + ex.GetType().ToString() +
"\n" + ex.StackTrace, "Exception");
}
}
我的讲师声称存在语法错误,我找不到。我以为如果有语法错误,该程序甚至无法编译。她说问题是我有一个void方法返回一个值。我的void方法(用于按钮单击的事件处理程序)是否在某种程度上不正确?
答案 0 :(得分:4)
不,没有语法错误。
您的讲师可能担心这部分,该部分出现在带有无效返回值的方法中:
bool isValidData()
{
return
IsPresent(txtLoanAmountLP) &&
IsPresent(txtInterstLP) &&
IsPresent(txtYearsLP) &&
IsDecimal(txtLoanAmountLP) &&
IsDecimal(txtInterstLP) &&
IsDecimal(txtYearsLP) &&
IsWithinRange(txtLoanAmountLP, 1, 999999) &&
IsWithinRange(txtInterstLP, 1, 18) &&
IsWithinRange(txtYearsLP, 1, 30);
}
这当然是最近引入的C#7局部函数。您可能会告诉她(因为您是学生,所以要有礼貌和谦虚)阅读:What's new in C# 7.0