2

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?

2
  • Why not modify the value once the return has completed, assuming your assigning it to a variable at the calling position.
    – Lazarus
    Sep 15, 2010 at 15:04
  • The point is to NOT modify any of the source code so as to avoid a huge recompile. Sep 15, 2010 at 15:16

5 Answers 5

1

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.

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

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
}
0
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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.