views:

220

answers:

4

Using C# in Visual Studio 2008 and stepping through a function in the debugger I get to the end of a function and am on the final curly brace } and about to return. Is there a way to find out what value the function is about to return?

This is necessary if the return value is calculated such as:

return (x.Func() > y.Func());
+1  A: 

You can put

(x.Func() > y.Func())

in a watch window to evaluate it, and see the result. Unless the statement is

return ValueChangesAfterEveryCall();

you should be fine.

MagicKat
This isn't a good idea if x.Func() or y.Func() have side effects. For instance, "return Database.CreateNewItemAndReturnItsID(someFoo)". If you copy that into a watch window, you'll end up hitting your database twice.
Seth Petry-Johnson
+2  A: 

It's a little low level, but if you switch to disassembly then you can single step through the instructions and see what the return value is being set to. It is typically set in the @eax register.

You can place a breakpoint on the ret instructions and inspect the register at that point if you don't want to single step through it.

Rob Walker
+2  A: 

I am still using VS 2003 with C++, so this may or may not apply. If you use the "auto" tab (near the "locals" and "watch" tabs), it will tell you the return value of a function once you return.

Jim Buck
Unfortunately, this isn't the case in C#. The auto tab isn't quite so useful. It's a shame.
Jeff Yates
How do you enable the auto tab? I see the locals and watch tabs but can't find the auto tab...
Guy
Debug menu, Windows->Autos
Jim Buck
Did this work out for you? Let me know if it didn't so that I can delete my answer if it's not applicable. Otherwise, feel free to accept my answer. ;)
Jim Buck
+1  A: 

I'd actually recommend refactoring the code to put the individual function returns in local variables. That way, yourself and others don't have to jump through hoops when debugging the code to figure out a particular evaluation. Generally, this produces code that is easier to debug and, consequently, easier for others to understand and maintain.

int sumOfSomething = x.Func();
int pendingSomethings = y.Func();
return (sumOfSomething > pendingSomethings);
Gabriel Isenberg
When you compile to Release, inlining occurs to eliminate these extra variables (outside of local try/catch blocks) so don't be afraid to make your code more readable by using more variables.
cfeduke
Does it really? I would expect the variables to still exist in Release mode as you have to put the results somewhere.
Jonathan Allen