开发者

Modifying method return value for managed code in Visual Studio 2010 debugger

Say I have this C# method:

public bool GetVal()
{
    return a1 == b1 || c1 开发者_运维知识库== d1 || GetE1() == GetF1(); // Illustrating complicated logic here..
}

I don't want to modify the content of the variables / return values of methods in the statement above, but want to return false to the method that's calling GetVal().

Is it possible to use the VS2010 debugger to somehow modify the return value in the same way that I can modify variable values on the fly? Perhaps modifying the call stack somehow?


It's not possible to modify the return value directly in this manner. Managed code has very limited / no support for seeing the return value of a function call as compared to C++.

But what you can do is go to the call site and modify the variable which is assigned the value of function call.


public bool GetVal()
{
    bool retval = a1 == b1 || c1 == d1 || GetE1() == GetF1();
// edit retval to be 'false' in the debugger now
    return retval;
}


I often find myself needing the same thing, unfortunately the only way to do this is to create a temporary variable which you can change via the Locals or Watch window before the function returns.

public bool GetVal()
{
    bool b = a1 == b1 || c1 == d1 || GetE1() == GetF1();
    return b;//Set breakpoint here
}


public bool GetVal()
{
    var result = a1 == b1 || c1 == d1 || GetE1() == GetF1(); // Illustrating complicated logic here..
    return result;
}

Set breakpoint on return line and modify result variable.


As the other answers noted:

public bool GetVal()
{
    bool result = a1 == b1 || c1 == d1 || GetE1() == GetF1();
    return result;
}

Works perfectly. However, in general code that strings together huge lines of if statements are much harder to read and debug anyway.

You should be breaking your function into steps rather than having it perform all of the steps in one line.

Another method to accomplish your task would be to set a breakpoint in your code at if(GetVal()) and simply modify the execution path from there, rather than trying to modify the return value, simply modify the state of affairs where the value is used.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜