|
Debugging VB .NET Applications
Whatever your project might be, debugging the application always helps you identify
the cause of errors/bugs and helps you fix them. You might be working on a Strategic
(new) project or Tactical (existing) project, debugging always comes handy. A simple scenario
could be a project where the code was already written by other developers and
you are finding it difficult to fix bugs. An example can be a huge windows application
where on one of the forms you are clicking a button and this button might save a message
along with the current system time. When you click this button you are able to save
a message but recording a wrong time. In such a scenario you are suppossed to
follow the debugging procedures and set breakpoints to find the code error. In this
case you need to set a breakpoint at button_save event and press F11 to see the flow
of the program and fix the bug. To understand debugging we will follow a simple
example.
Here, we will work with a sample application to understand debgging. Open a new
Windows Form and place 2 buttons, 2 labels, a datetimepicker control and 2 text boxes.
The form in design mode should like like the image below.
The codebehind for this form is as follows:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load
TextBox2.Text = DateTimePicker1.Value.Date
End Sub
Private Sub getTxt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles getTxt.Click
TextBox1.Text = "Debugging Test"
End Sub
Private Sub clr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles clr.Click
TextBox1.Text = " "
TextBox2.Text = " "
End Sub
End Class
|
For the code listed above we will set a breakpoint at the clr_click event of the clear
button in the code editor window. To set a breakpoint, point the mouse pointer towards
the left of code and click it to see a red circle as shown in the image below.
Once you set the breakpoint, run the application by hitting F5. Current system date
loads into textbox2. Click the get text button to fill textbox1 with text. Now,
click the clear button to clear both the textboxes. Once you click the clear button,
control shifts to the code editor, highlights the click event of the clear button
with a yellow background and looks like the image below.
Now, when you place the mouse pointer on Textbox1.Text it shows the current value
textbox1 is holding in it's text property which is "Debugging Text". Similarly,
textbox2 shows the curent date when you place the mouse pointer on TextBox2.Text.
Now, press the F11 (Step Into) key to see the control flow. The control shifts
to TextBox1 and it still shows the text Debugging Test. Press F11 again and now the
control shifts to TextBox2. Now, if you place the mouse pointer on TextBox1.Text
it shows that the text property is set to null which means that the control has set
the text property of TextBox1 to null. The image below shows that.
At this point, textbox2 still holds the current system date. Press F11 again and now
the control shifts from textbox2 to End Sub. Now, if you place the mouse pointer
on TextBox2.Text it shows it's text property as null. If you press F11 once again
the control finishes End Sub and jumps back to the form which is running. What
we saw now is the passing of program flow when we set breakpoints and how the
values change in the block while the control executes line by line.
With the above example we understood how debugging can help us find and fix errors.
This was a small example but in real time applications it will be very helpful in
finding and fixing some hard to find bugs. In a huge windows application there may
be many forms and many of those forms might get inherited by other forms in a different
solution or a different project in the same solution. Setting breakpoints at certain
points on the form and running the application will help you to get to know how the program
control flows, what method on one form is calling on another form and
so on and helps you understand the logic n fix errors.
|