如果信息不正确,如何防止保存两个功能。 [英] How Do I Prevent The Two Functions From Saving If One Has Incorrect Information.
问题描述
Business_Logic.Employee objt = new Business_Logic.Employee();
objt.Position_Number = TxtPositionNum.Text;
objt.Grade = Txtgrade.Text;
objt.StartDate = TxtStartDate.Text;
objt.Save_Position();
Business_Logic.Employee obje = new Business_Logic.Employee();
obje.Position_Number = TxtPositionNum.Text;
obje.Cell_Number = TxtCellNumber.Text;
obje.Email = TxtEmail.Text;
obje.Save_Employee();
Business_Logic.Employee objt = new Business_Logic.Employee();
objt.Position_Number = TxtPositionNum.Text;
objt.Grade = Txtgrade.Text;
objt.StartDate = TxtStartDate.Text;
objt.Save_Position();
Business_Logic.Employee obje = new Business_Logic.Employee();
obje.Position_Number = TxtPositionNum.Text;
obje.Cell_Number = TxtCellNumber.Text;
obje.Email = TxtEmail.Text;
obje.Save_Employee();
推荐答案
修改Save_Position方法以返回bool值:true表示确定,false表示发生错误
然后检查你的代码:
Modify the Save_Position method to return a bool value: true for OK, false for "an error occured"
Then check in your code:
objt.Position_Number = TxtPositionNum.Text;
objt.Grade = Txtgrade.Text;
objt.StartDate = TxtStartDate.Text;
if (objt.Save_Position())
{
Business_Logic.Employee obje = new Business_Logic.Employee();
obje.Position_Number = TxtPositionNum.Text;
obje.Cell_Number = TxtCellNumber.Text;
obje.Email = TxtEmail.Text;
obje.Save_Employee();
}
或者,在Save_Position方法中抛出异常,并在调用片段的代码中捕获它。然后它可以向用户报告问题。
Alternatively, throw an exception in your Save_Position method, and catch it in the code which calls your fragment. It can then report a problem to the user.
使用flag变量检查是否发生错误。如果没有错误,则保存代码,否则忽略它。
Use the flag variable to check whether error is occurred or not. If there no error then save the code otherwise ignore it.
这篇关于如果信息不正确,如何防止保存两个功能。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!