views:

262

answers:

3

I have an object i have to watch a function output of pretty many times through the watch window. The problem is that i have to press the refresh button every time i step because "this expression causes side effects and will not be evaluated". Is there any way to tag a function with something like [DoesNotCauseSideEffects] so the watch automatically can evaluate it every time i make a step?

I'm coding C# in VS2008 or 2010.

Edit: The function looks like this and does, as you can see, not cause any side effects.(x,y,z are all doubles)

public override string ToString()
{
    return "{ " + x.ToString(".00") + ", " + y.ToString(".00") + ", " + z.ToString(".00") + "}";
}
+3  A: 

No this is not possible. The determination of whether or not a function will have side effects is actually made by the language specific expression evaluator.

Both C# and VB.Net have different EE's and each has it's own determination of whether on not a function causes side effects. In general though, they both consider an explicit function call to have side effects and will not process them in situations, such as stepping, where the debugger wants to disable side effects.

The easiest way to work around this though is to use a Property. By default Property instances are not considered to have side effects and they will evaluate on step.

EDIT

Wanted to clear up some misconceptions. Neither the debugger or expression evaluator will do any sort of method inspection to determine if a function has side effects. They simply note the presence of a function and label it as potentially having side effects. They don't attempt to inspect the code of the method in any way.

JaredPar
A: 

Have you tried making it so that the function does not modify any class properties or global variables? My off-the-cuff definition of "side effects" is "changes to the execution environment that are made during the execution of a function". So this function causes side effects:

void addValues(int operand1, int operand2) {
  globalSum = operand1 + operand2;
}

but this function doesn't:

int addValues(int operand1, int operand2) {
  return operand1 + operand2;
}

It's always better to avoid side effects whenever possible. They can cause problems in debugging. See this wikipedia article.

eeeeaaii
This doesn't work. The EE's don't attempt to inspect the function in any way, they simply note it's not a property and label it as potentially having side effects.
JaredPar
A: 

You can append ",ac" to the watch expression to have it automatically refresh the value

x.ToString(),ac

See http://msdn.microsoft.com/en-us/library/e514eeby.aspx for more information and other format specifiers.

Scott Dugas