Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inspect return value without assign it to a variable

I have following code snipped:

...
var tpc = new ThirtPartyClass();

tpc.ExecuteCommand();
tpc.ExecuteCommand();
...

The ExecuteCommand() method returns a int value with some information. For debugging, I want to know this return values. But I don't want to assign the result to a variable (var result = tpc.ExecuteCommand()).

Is there in VisualStudio 2010 a possibility during debugging, to inspect this return value without assign it to a temporary variable?

Thanks in advance for your suggestions

edit: Finally, this function has been added to VS2013

like image 753
rhe1980 Avatar asked Jan 17 '12 11:01

rhe1980


People also ask

How do you store a value returned from a function?

how can I store and use the return value in a variable? you can easily do var ddResult = formatDateToString(); the return value dd will now be in ddResult.

How do you find the return value?

There are 2 places where we can see the method return value: In the Debugger Immediate window, using the $ReturnValue keyword. To open the Immediate window while debugging, choose Debug -> Windows -> Immediate (or press keyboard shortcut: Ctrl + Alt + I).

Can we assign value in return statement?

return isn't like a function or a variable that gives you a value, it only takes values. You put it in a function, and whatever you return is what you get when you use the function later on. It's always the last thing a function does, when it returns, it stops executing.

Can we define a function without return statement?

If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined.


1 Answers

You can do that with IntelliTrace in VS2010, by switching to the "Calls View" then checking the Autos window:

enter image description here

But even without that, don't worry about it; if you don't use the variable (except by looking in the debugger when paused), then in a release build it will be removed and replaced with just a "pop" (which is what you get if you don't catch the return value in the first place).

So:

static void Main()
{
    int i = SomeMethod();
}

Compiles as:

.method private hidebysig static void Main() cil managed
{
    .entrypoint
    .maxstack 8
    L_0000: call int32 Program::SomeMethod()
    L_0005: pop 
    L_0006: ret 
}

note no .locals and no stloc.

For Resharper, use:

// ReSharper disable UnusedVariable
    int i = SomeMethod();
// ReSharper restore UnusedVariable
like image 190
Marc Gravell Avatar answered Oct 18 '22 22:10

Marc Gravell