|
Debugging VB .NET Applications
This section focuses on Breakpoints and the Debug Menu found in Visual Studio
.NET, the items within it and their use.
Break Mode
Break Mode allows us to halt code execution and execute code one line at a time. In
break mode we can examine the values of application variables and properties. Visual
Studio .NET enters break mode under any of the following circumstances:
- When we choose Step Into, Step Over or Step out from the Debug menu
- Execution reaches a line that contains an enabled breakpoint
- Execution reaches a Stop statement
- An unhandled exception is thrown
The image below shows the code designer window in Break Mode.
Once in Break mode we can use the debugging tools to examine our code. The image below
shows the features on Debug menu.
Debug Menu Items from the above image summarized:
Windows: Opens a submenu that allows us to choose a debugging window
to view.
Start/Continue: Runs the application in debug mode. In Break mode,
this option continues program execution.
Break All: Causes program execution to halt and enter break mode
at the current program line. You can choose continue to resume execution.
Stop Debugging: Stops the debugger and return to design mode in Visual
Studio.
Detach All: Detaches the debugger from all processes it is debugging.
This closes the debugger without halting program execution.
Restart: Terminates and restarts application execution.
Apply Code Changes: Works for C/C++ programming. Doesn't work for
VB and C#.
Processes: Displays the processes window.
Exceptions: Displays the exception window.
Step Into: Runs the next executable line of code. If the next line
calls a method, Step Into stops at the beginning of that method.
Step Over: Runs the next executable line of code. If the next line
calls a method, Step Over executes that method and stops at the next line within the
current method.
QuickWatch: Displays the QuickWatch window.
New Breakpoint: Displays the New Breakpoint Window.
Clear All Breakpoints: Removes all breakpoints from the application.
Disable All Breakpoints: Disables all breakpoints, but does not delete
them.
There are some more debugging functions that can be accessible by right-clicking on
an element in the code window and choosing a function from the pop-up menu. The image
below displays those functions and summarizes them.
Insert Breakpoint: Inserts a breakpoint at the selected line.
New Breakpoint: Displays the new Breakpoint window. Identical to
the Debug menu item of the same name.
Add Watch: Adds the selected expression to the watch window.
QuickWatch: Displays the QuickWatch window.
Show Next Statement: Highlights the next statement to be executed.
Run To Cursor: Runs program execution to the selected line.
Set Next Statement: Designates the selected line as the next line
of code to be executed. The selected line should be in the current procedure.
Breakpoints
We can insert breakpoints at lines of code that will always cause the application
to break in the debugger. Breakpoints are used to set lines of code that will halt
code execution. There are four types of breakpoints as discussed below:
Function breakpoints: Causes the application to enter Break mode
when the specified location in the function is reached.
File breakpoints: Causes the application to enter Break mode when
the specified location in the file is reached.
Address breakpoints: Causes the application to enter Break mode when
the specified memory address is reached
Data breakpoints: Causes the application to enter Break mode when
the value of a variable changes. Not available in VB and C#.
The image below displays the code designer when a breakpoint is inserted.
When setting breakpoints with the New Breakpoint window we can attach conditions that
determine whether or not execution halts when the breakpoint is reached. Pressing
the Condition button displays the Breakpoint Condition window which allows us to designate
an expression and causes the breakpoint to be active only if the breakpoint is true
or if the expression changes.
Breakpoints Window
The Breakpoints Window lists all of the breakpoints in the project, like, where the
breakpoint is located and any conditions attached to it. We can disable a breakpoint
by clearing the check box next to it. The button in the Breakpoints window allows
us to create a new breakpoint, delete a breakpoint or clear or disable all breakpoints.
The Columns drop-down menu allows us to choose additional columns of information about
the breakpoints. The image below displays a breakpoints window.
Removing and Disabling Breakpoints
We can remove a breakpoint by clicking on the circle like display towards the left
side of the code window where the breakpoint is inserted. To disable a breakpoint,
right-click the breakpoint in the code editor and select "Disable Breakpoint".
|