6

Consider the following piece of code: my code

As you can see we are on line 28. Is there any way to see the return value of the function at this point, without letting the code return to the caller function?

Foo.Bar() is a function call which generates a unique path (for example). So it's NOT constant.

Entering ?Foo.Bar() in the immidiate window doesn't work either, since that reevaluates the code:

?Foo.Bar()
"80857466"
?Foo.Bar()
"2146375101"
?Foo.Bar()
"1106609407"
?Foo.Bar()
"792759112"

In VB.NET it's possible by entering the function's name in the Watch, which will then threat it as a variable.

But in C# this is not possible, any other tips?

PS: rewriting is not an option.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Anemoia
  • 7,928
  • 7
  • 46
  • 71
  • 2
    yup.. just select the Foo.Bar() and right click on it and go for the quick watch. – Malcolm Apr 30 '10 at 08:13
  • @Malcolm: quick watch reevaluates. you might end up with a different response, depending on what `Bar()` does – David Hedlund Apr 30 '10 at 08:16
  • I agree with you David but if he puts the breakpoint as cgreeno as answered below it will work. :) – Malcolm Apr 30 '10 at 08:46
  • @Malcolm: granted, but there's always the possibility that `Bar` is not a method that you can step into. it might be a call to somebody elses DLL. i quite often see solutions like "return the id of the newly inserted db row". in those cases the accepted answer would be appropriate. it just seems like a common enough scenario that you could expect that there'd be a way to inspect the value being returned, even if it's a complex object (which is not covered by the accepted answer)... – David Hedlund Apr 30 '10 at 09:41

2 Answers2

3

Answer found here: VS get returned value in C# code?

It is actually visible. Debug + Other Windows + Registers. Look at the value of EAX (RAX in x64). The value of simple integral types are returned in the EAX register. Long in EDX:EAX. Floating point in STx (XMM00 in x64).

Community
  • 1
  • 1
Daniel Renshaw
  • 33,729
  • 8
  • 75
  • 94
  • You're right, doesn't help much for objects. You could always go query memory at the address stored in that register but that's hardly easy! I can't find any alternative so I guess you'd have to fall back on storing the result in a variable before returning it. See the answers to the linked SO question for a link where you can ask MS to do something about this. – Daniel Renshaw Apr 30 '10 at 08:36
2

Assuming you are using visual studio, you could use the Immediate window. If you type Foo.Bar(); in the Immediate window you will get the result you are after, if you don't want it to re-evaluate stick a break point in the appropriate spot IE either before it evaluates the first time or in Foo.Bar() itself.

cgreeno
  • 31,943
  • 7
  • 66
  • 87