The first rule of debugging is "don't". Think of it as being like having your car break down. The goal is to maintain your car and keep that from ever happening to you. If it does, it seems like it always happens at the worst possible time. Programs always crash in production when you're a thousand miles away on vacation. (I remember one case where someone was called back from a cruise ship in the ocean to fix a problem.)
And just like your car, you can prevent most bugs.
- Write meaningful comments.
- Take advantage of the VB.NET compiler with "Option Explicit" and "Option Strict".
- Include exception handling in your program.
And, of course,
- Test! Test! Test!
Highly critical systems - for example, one that controls a rocket launch - are tested using carefully designed data that checks every part of the program. Whenever any change is made to the program, the entire test is run to make sure it still works the way it should. The first launch of the European Ariane 2 rocket was a failure, at enormous cost, because the program that controlled the launch wasn't completely tested.
Use Comments
In Lesson 3's CalcDateDiff_Click subroutine, it isn't obvious that calculating the difference between two Date types creates a TimeSpan type. So you might want to help someone debugging your program later (it will probably be you) by adding a helpful hint:
' The minus operator here creates a TimeSpan type
DaysBetweenDatesDisplay.Text = _
CStr((SecondDateSave - FirstDateSave).TotalDays)
Everything following the single quote on that line is ignored by the compiler.
Option Explicit and Option Strict
Option Explicit requires all variables to be declared before they're used. That's the default for VB.NET so you don't have to remember to turn it on. But Option Strict is Off by default, so you do have to add a statement to use it. Because these statements are actually instructions to the Visual Basic compiler, they have to be coded before any other source code statements.
Option Strict On
Public Class DebugEx
Dim FirstDateSave As Date
. . .
VB.NET will convert from one data types to another when possible. For example, you can declare one variable as an Integer type and another as a Double. (Look up "Double data type" at MSDN.microsoft.com if you have questions about what it is.) But you can lose information with a "narrowing" conversion.
Consider this code:
Dim myInteger As Integer = 10
Dim myDouble As Double = 10.5
myDouble = myInteger
myInteger = myDouble
Console.Writeline("myInteger: " & myInteger & _
" -- myDouble: " & myDouble)
This gives you this result in the Output window:
myInteger: 10 -- myDouble: 10
Information has been lost. There is no trace of the fact that myDouble used to be .5 more.
But when Option Strict is set to On, as soon as you code the "narrowing conversion" in the fourth line above, Visual Studio tells you about it with a "blue squiggly line" under the problem conversion and a tooltip error message:
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Exception Handling - Try-Catch Blocks
Exception handling is is program code that anticipates a problem and does something about it. There's a good example of something that might go wrong in the Days Between Dates program. What if the user clicks the button to calculate a difference before selecting both dates? Here's what happens now:
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
A large negative number obviously isn't correct. In VB.NET, checking for problems like this may take the form of a Try-Catch block. Here's what one looks like:
Try
<code that might have a problem>
Catch exceptionVar as exctptionType
<code that does something with the problem>
Finally
<code that will execute no matter what>
End Try
Here's how this particular problem could be handled:
Try
' The minus operator here creates a TimeSpan type
If FirstDateSave = #12:00:00 AM# Or _
SecondDateSave = #12:00:00 AM# Then
Throw _
New ApplicationException("Date Not Selected")
End If
DaysBetweenDatesDisplay.Text = _
CStr((SecondDateSave - FirstDateSave).TotalDays)
Catch ex As ApplicationException
MsgBox("At Least One Date was Not Selected", _
MsgBoxStyle.Exclamation, _
"Date Not Selected")
End Try
And here is the result:
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Try-Catch is used more often to check for system errors - such as an error reading a file - not application errors as shown here. In fact, it's not really necessary to use one here. In this program, you could just execute the MsgBox statement instead of "throwing" an exception. But for more complex programs, this is a great technique to learn. You might, for example, enclose about a hundred lines of processing where dozens of different things could go wrong in a single block and throw different errors for everything that is a problem. Then "catch" all of the thrown errors at the end in a single exception processing module.

