如果出现一个条件,我必须停止当前方法调用的执行并返回到方法调用之前的状态.我可以这样做..假设ai正在执行一些示例方法并且出现一个条件而我正在提示一个消息框然后我想在此函数调用之前返回状态
如果我正确理解你,如果某些条件成立,你希望撤消对某些变量所做的更改吗?如果是这种情况,您将需要存储所有变量(或整个类)的副本.然后,如果你的条件成立,你必须在从函数返回之前将所有这些变量恢复到它们的初始状态.它会是这样的:
// In order to clone your variable, you may need to inherit from
// ICloneable and implement the Clone function.
bool MyFunction(ICloneable c)
{
// 1. Create a copy of your variable
ICloneable clone = c.Clone();
// 2. Do whatever you want in here
...
// 3. Now check your condition
if (condition)
{
// Copy all the attributes back across to c from your clone
// (You'll have to write the ResetAttributes method yourself)
c.ResetAttributes(clone);
// Put a message box up
MessageBox.Show("This failed!");
// Now let the caller know that the function failed
return false;
}
else
{
// Let the caller know that the function succeeded
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10708 次 |
| 最近记录: |