Solved

Setting watches in Visual Studio while debugging?

Posted on 2014-12-04
4
140 Views
Last Modified: 2014-12-05
I'm not real sure how the "Watches" work in VS. I always thought that you could specify a variable/object to watch and then when the value changes, the program would go into debug mode and stop at the point of change without having to set any breakpoints. Is this correct? If so, I'm doing something wrong somewhere. If not, what is the correct way to utilize "Watches"?

Thanks!
0
Comment
Question by:BlakeMcKenna
4 Comments
 
LVL 14

Assisted Solution

by:brendanmeyer
brendanmeyer earned 167 total points
ID: 40482027
watches are used when you are already in debug mode,  when it pauses, or when you step through the code

Although you can set Conditional Breakpoints in the code,
set a normal breakpoint, and then right click the circle, and you can change it to conditional, ans set the conditions
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 166 total points
ID: 40482186
A Watch is not a way to break your code. It is simply a tool to easily see the values of variables and expressions when you pause the program in Debug mode or when a runtime error is encountered when running the program from Visual Studio.

You can also do this in other ways, such as moving the cursor over the variable or using data tips. But the watches have the advantage that you can see their values even when the code containing the variable or the expression is scrolled off the screen.

They enable you to easily give a look at the content of an array or some complex structures such as a DataTable.

You can also use them to change the value of a variable during a Debug session, which can be useful for some debugging or analysis situations.

Because you can have up to 4 watch windows (through the Debug menu while in Debug mode), you can also group them, enabling you as an example, to have all your database watches in one window and all the variables dealing with the form you are currently working on in another one,

But they cannot be used as breakpoints. Breakpoints have to be set manually or through code (with the Debug.Assert command). Manually, you set them by clicking in the grey margin at the left of the line on which you want to stop. As pointed to by brendanmeyer, you can set conditions through a right click on the breakpoint after it has been set. You can define that a breakpoint will trigger only if a value has changed since the last time a specific line is executed.

But there is no way to set a breakpoint "when a value change", that is to automatically break at the point where the change occurs. This is bad, because such a feature was available in the 90's in VB classic. I miss that feature a lot. But you have to understand that VB6 was interpreted while VB.NET is compiled when running in the debugger. It's easy for an interpreter to see if a variable change on a given line. Debugging would probably become a pain (very slow) if it was implemented in an environment where compiled code is running.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 167 total points
ID: 40482295
But there is no way to set a breakpoint "when a value change", that is to automatically break at the point where the change occurs. This is bad, because such a feature was available in the 90's in VB classic.
Er, if you have to know the instant a variable changes, and you don't know what code is executing the change, then you have a bad design. If you have so many places in your class that can update a variable that you cannot make sense of which place is updating the value, then you should probably introduce a property which sets the value, and then update the variable via the property--at least then you can break in the property's setter, and you'll be able to inspect call stack at that point.
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40482768
Thank you...
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now