Take the following function:
DataTable go() {
return someTableAdapter.getSomeData();
}
When I set a breakpoint in this function, is there a possibility to inspect the returned value? go()
is directly coupled to a datagrid in an .aspx
page.
The only way to inspect the returned datatable is to use a temporary variable. However, that's a bit inconvenient. Isn't there another way?
Yeah, by switching to VB.NET. ;P (You did just say "Visual Studio". ;)
For as long as I can remember (from Visual Basic through all versions of VB.NET), you can simply query the function name. It "functions" like a local variable that's implicitly declared at the start of the function and its current value is also used as the return value whenever the function exits via non-return statement means (i.e.
Exit Function
or just falling through) and of course, when the return statement is used.It is also set to the return statement's expression. Just like a local variable, its value can be inspected at any point of execution inside the function (including after the return statement is executed). C# doesn't have this and should.
That little VB.NET feature (plus the
Exit Function
statement which it enables - another feature C# doesn't have and should) is very useful in a form of defensive programming I practice where I always initialize the function name to the failure/default value as the first statement. Then, at any failure point (which normally occurs much more often than success points), I can simply call theExit Function
statement (i.e. without having to duplicate the failure / default expression or even a constant/variable name).According to Microsoft, there is no way to implement this reliably with managed code. This is a problem they are aware of and are working on:
https://connect.microsoft.com/VisualStudio/feedback/details/597933/add-a-return-pseudo-variable-to-the-visual-studio-debugger-for-net-code
Not that I know of. Note that if you do add a variable, it will get removed by the compiler in release builds anyway...
Update: This functionality has been added to VS2013. You can see the return values in the autos windows or use
$ReturnValue
in the watch/immediate window.The value can only be seen directly after returning from the function, thus the easiest way to access it is by putting a breakpoint on the function call and step over (F10) the call.
Update for VS2015: boo! unfortunately, it doesn't appear to be in VS2015 (devenv v14)
Update for VS2017: it's back. (devenv v15)
There are a lot of workarounds, but none seems satisfactory.
To quote John Skeet below (comment on a now-deleted answer):
In theory, the debugger could have a
return
-variable. After all: it's just a variable on the stack:So consider this a feature request for Visual Studio.
You can also ask to evaluate the value in the intermediate window as well, if it does not set flags or other variables, but only returns something.
You could try to select
"someTableAdapter.getSomeData();"
, right click on it, and go for Quick Watch.