我有两个不同的按钮点击,调用相同功能的修改版本...
private void button1_Click(object sender, EventArgs e)
{ SendEmail(); }
private void button2_Click(object sender, EventArgs e)
{ SendRevisedEmail();}
public void SendEmail()
{
DataManip(ref item1, ref item2); //This is where I'd like the next 2 functions to not process if this one fails.
UpdateDB(ref item1, ref item2);
sendTechEmail(ref item1, ref item2);
}
public void SendRevisedEmail()
{
DataManip(ref item1, ref item2); //This is where I'd like the next 2 functions to not process if this one fails.
UpdateDB2(ref item1, ref item2);
sendRevisedTechEmail(ref item1, ref item2);
}
Run Code Online (Sandbox Code Playgroud)
在DataManip函数中,我让它对表单执行一些检查并设置为抛出一个弹出消息并返回; 如果它没有出来flag1 = true.
public void DataManip(ref string item1, ref string item2)
{
bool flag1 = false;
foreach (Control c in groupBox1.Controls)
{
Radiobutton rb = c as RadioButton;
if rb != null && rb.Checked)
{
flag1 = true;
break;
}
}
if (flag1 == true)
{
//Manipulate Data here
}
else if (flag1 != true)
{
MessageBox.Show("You didn't check any of these boxes!");
return;
};
}
Run Code Online (Sandbox Code Playgroud)
截至目前,flag1签入DataManip工作正常.如果缺少groupBox1中的条目,我可以验证它是否处理数据更改.
问题是在SendEmail()和SendRevisedEmail()函数内部,它仍然处理其他函数的调用DataManip(ref item1, ref item2).
如何导致错误退出DataManip并阻止/跳过执行的其他两个函数调用?
如何导致错误退出DataManip并阻止/跳过其他两个函数调用?
你有几个选择:
bool.这将允许您返回值,无论方法是否成功.请注意,您可能需要检查代码中的其他一些奇怪之处.很少你应该把所有东西都带走ref.此外,在操作数据的同一方法中使用消息框类型通知通常不是一个好主意 - 您可能需要考虑将值的验证/提取与数据操作分开.
| 归档时间: |
|
| 查看次数: |
1096 次 |
| 最近记录: |