首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何停止当前方法调用的执行

如何停止当前方法调用的执行
EN

Stack Overflow用户
提问于 2009-04-14 06:37:27
回答 7查看 10.9K关注 0票数 0

如果出现条件,我必须停止当前方法调用的执行,并返回到方法调用.how之前的状态。假设我正在执行某个示例方法,并且发生了一个条件,我正在提示一个消息框,然后我想返回到此函数调用之前的状态

EN

Stack Overflow用户

发布于 2009-04-14 06:53:14

如果我没理解错的话,你希望在某些条件为真的情况下撤销对某些变量所做的更改?如果是这种情况,您将需要存储所有变量(或作为一个整体的类)的副本。然后,如果您的条件为真,那么在从函数返回之前,您必须将所有这些变量恢复到它们的初始状态。它应该是这样的:

代码语言:javascript
运行
复制
// 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;
    }
}
票数 5
EN
查看全部 7 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/746548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档