views:

44

answers:

5

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?

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

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
}
Brian R. Bondy
+1  A: 

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.

JaredPar
A: 
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.

Alex Reitbort
A: 

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.