Why does tsSource[j].Text.ToUpper() causes side effects

  • Thread starter Thread starter Academia
  • Start date Start date
A

Academia

I get the following watch message:

tsSource[j].Text.ToUpper() This expression causes side effects and will
not be evaluated string


The Text is &Edit



Do you know what are the side effects?



Thanks
 
I get the following watch message:

tsSource[j].Text.ToUpper() This expression causes side effects and will
not be evaluated string


The Text is &Edit



Do you know what are the side effects?

There probably aren't any in this case, but the debugger is not smart enough
to be able to tell - so it prefers to err on the safe side.

Quoting from http://msdn2.microsoft.com/en-us/library/a7a250bs.aspx :

"A side effect occurs when evaluating an expression changes the value of data
in your application.

Side effects are something to watch for if you are evaluating expressions in
the debugger. If you evaluate an expression in the Watch window or the
QuickWatch dialog box and the expression has side effects, you might change
the value of variables in another part of your program without realizing it.
Side effects can make debugging more difficult by creating the appearance of
bugs where none exist or masking the appearance of real bugs.

One common cause of side effects is evaluating a function call in a debugger
window. Such evaluations are usually noticeable. A more subtle cause of side
effects is the evaluation of properties and other implicit function calls in
managed code.

The debugger cannot tell whether a property evaluation or implicit function
call has side effects. Therefore, by default, the debugger does not evaluate
implicit function calls automatically. Property evaluation is allowed by
default, but can be turned off in the Options dialog box. When a function call
or property has not been evaluated, a refresh icon appears. You can manually
evaluate the expression by clicking the refresh icon. For details, see How to:
Refresh Watch Values."

Regards,
Gilles.
 
I get the following watch message:

tsSource[j].Text.ToUpper() This expression causes side effects and will
not be evaluated string

The Text is &Edit

Do you know what are the side effects?

If the type of the Text property is actually String (and that seems like a
safe assumption, given your post), then there should not be any
side-effects.

Do you only get that message with the full expression? That is, using
"tsSource[j]" or "tsSource[j].Text" works okay (other than not converting
the string to uppercase, of course)?

Seems like the debugger's being overly cautious to me, though why that
would be, I don't know. I can't reproduce the message here with a simple
call to ToUpper() in the watch window on an arbitrarily chosen string
value in a test program.

It seems to me that your best bet would be to create and post a
concise-but-complete sample of code that reliably demonstrates the
problem. Also, be sure to provide specifics regarding which version of VS
you're using, and maybe even which .NET version you're using.

Pete
 
Thanks for the help

Gilles Kohl said:
I get the following watch message:

tsSource[j].Text.ToUpper() This expression causes side effects and will
not be evaluated string


The Text is &Edit



Do you know what are the side effects?

There probably aren't any in this case, but the debugger is not smart
enough
to be able to tell - so it prefers to err on the safe side.

Quoting from http://msdn2.microsoft.com/en-us/library/a7a250bs.aspx :

"A side effect occurs when evaluating an expression changes the value of
data
in your application.

Side effects are something to watch for if you are evaluating expressions
in
the debugger. If you evaluate an expression in the Watch window or the
QuickWatch dialog box and the expression has side effects, you might
change
the value of variables in another part of your program without realizing
it.
Side effects can make debugging more difficult by creating the appearance
of
bugs where none exist or masking the appearance of real bugs.

One common cause of side effects is evaluating a function call in a
debugger
window. Such evaluations are usually noticeable. A more subtle cause of
side
effects is the evaluation of properties and other implicit function calls
in
managed code.

The debugger cannot tell whether a property evaluation or implicit
function
call has side effects. Therefore, by default, the debugger does not
evaluate
implicit function calls automatically. Property evaluation is allowed by
default, but can be turned off in the Options dialog box. When a function
call
or property has not been evaluated, a refresh icon appears. You can
manually
evaluate the expression by clicking the refresh icon. For details, see How
to:
Refresh Watch Values."

Regards,
Gilles.
 
Without the ToUpper() I get no message.

Thanks for the answer




Peter Duniho said:
I get the following watch message:

tsSource[j].Text.ToUpper() This expression causes side effects and will
not be evaluated string

The Text is &Edit

Do you know what are the side effects?

If the type of the Text property is actually String (and that seems like a
safe assumption, given your post), then there should not be any
side-effects.

Do you only get that message with the full expression? That is, using
"tsSource[j]" or "tsSource[j].Text" works okay (other than not converting
the string to uppercase, of course)?

Seems like the debugger's being overly cautious to me, though why that
would be, I don't know. I can't reproduce the message here with a simple
call to ToUpper() in the watch window on an arbitrarily chosen string
value in a test program.

It seems to me that your best bet would be to create and post a
concise-but-complete sample of code that reliably demonstrates the
problem. Also, be sure to provide specifics regarding which version of VS
you're using, and maybe even which .NET version you're using.

Pete
 
Academia said:
I get the following watch message:

tsSource[j].Text.ToUpper() This expression causes side effects and
will not be evaluated string

I rather suspect the message is "This expression causes no side effects".

What you've asked for is:
Find the jth item.
Read it's Text property.
Make an uppercase copy.
Throw away the result.
 
Peter said:
Academia said:
I get the following watch message:

tsSource[j].Text.ToUpper() This expression causes side effects and
will not be evaluated string

I rather suspect the message is "This expression causes no side
effects". What you've asked for is:
Find the jth item.
Read it's Text property.
Make an uppercase copy.
Throw away the result.

"Watch message" implies debugger. What you're describing would be a
problem if the compiler were looking at the expression, but here the
result of the expression isn't thrown away, it's (in theory)
displayed to the user.

You're right. I didn't connect "watch message" with the debugger watch
window.
In practice, it seems that it's not being displayed, because the
debugger believes it causes side-effects and thus won't evaluate it. Why
that should be, I don't know...I can't reproduce the same problem
(my debugger seems perfectly happy to put a call to ToUpper() in the
watch window, even when the source is some variable or property). But it's
not hard to believe that the debugger is hesitate to call a
method in the watch window because of the possibility of a
side-effect.

It will display, it typically won't update without being asked, though.

There are calls to an indexer, property get accessor, and method in that
expression, which could be too much for the debugger to analyze.
 
Peter said:
[...]
In practice, it seems that it's not being displayed, because the
debugger believes it causes side-effects and thus won't evaluate
it. Why that should be, I don't know...I can't reproduce the same
problem (my debugger seems perfectly happy to put a call to
ToUpper() in the watch window, even when the source is some
variable or property). But it's
not hard to believe that the debugger is hesitate to call a
method in the watch window because of the possibility of a
side-effect.

It will display, it typically won't update without being asked,
though.

Right. But the OP apparently can't even get it to display.
There are calls to an indexer, property get accessor, and method in
that expression, which could be too much for the debugger to analyze.

The OP says that as long as he removes the call to ToUpper(), the
debugger stops complaining. An indexer and a property are,
ultimately, just methods. So why does the addition of the third
method cause an issue?

Apparently the debugger assumes that property access (including indexed
properties) has no side effects, and method access does. Without attributes
to give it a hint or full analysis of the code I don't see how it could do
better.

The real problem is that this assumption is violated in a rather big way by
some BCL classes. Ever got an InvalidOperationException on
Control.BeginInvoke because the handle wasn't created? Well, the handle
wasn't created when the exception occured, but it is now because the
debugger happily read the .Handle property.

The fact that reading the Handle property creates the handle, but the
CreateControl method doesn't (if Visible is false) is just really poor
design in my opinion. Add to that the fact that the short description which
appears in intellisense is "Forces the creation of the control, including
the creation of the handle and any child controls" and you have to pull up
MSDN to learn about the Visible condition, and it's a real problem.
 
Back
Top