如果出现条件,我必须停止当前方法调用的执行,并返回到方法调用.how之前的状态。假设我正在执行某个示例方法,并且发生了一个条件,我正在提示一个消息框,然后我想返回到此函数调用之前的状态
发布于 2009-04-14 06:53:14
如果我没理解错的话,你希望在某些条件为真的情况下撤销对某些变量所做的更改?如果是这种情况,您将需要存储所有变量(或作为一个整体的类)的副本。然后,如果您的条件为真,那么在从函数返回之前,您必须将所有这些变量恢复到它们的初始状态。它应该是这样的:
// 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;
}
}https://stackoverflow.com/questions/746548
复制相似问题