Let's write a short program that we can use to exercise the debugger. The form we'll use is shown in Figure 19.2.
Figure 19.2. The form used for testing the debugger.
The large text box is named txtOuput and it has its Multiline property set to True . The Start button is named btnStart . (I don't really have to tell you about btnExit any more, do I?) Note that there's a borderless label named lblProgress between the two buttons . The program writes some loop iteration data on the label as the program executes.
The code for the program is presented in Listing 19.1. There really isn't much to the code.
Listing 19.1 Source Code for the Debugger Test Program
Public Class frmViewError Inherits System.Windows.Forms.Form Private Const UNUSED_ERROR_NUMBER As String = "Application-defined or _ object-defined error." ' Windows Form Designer generated code Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles btnExit.Click Me.Dispose() End Sub Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles btnStart.Click Dim i As Integer, ValidErrors As Integer Dim temp As String ValidErrors = 0 For i = 0 To 1000 ' Those below 1000 are reserved temp = ErrorToString(i) If temp <> UNUSED_ERROR_NUMBER And temp <> "" Then txtOutput.Text &= Format(i, "000") & " " & ErrorToString(i) & vbCrLf ValidErrors += 1 End If lblProgress.Text = "Processing error number: " & CStr(i) Me.Update() Next lblProgress.Text = "Total useable error messages: " & CStr(ValidErrors) End Sub End Class
Microsoft uses a number of predefined runtime error messages. The program presented in Listing 19.1 simply displays the runtime error number and its associated error message. It does this by iterating through a For - Next loop while calling the ErrorToString() method of the Err object.
Microsoft documents that it has reserved the first 1,000 error numbers for its own use. Therefore, the loop tests all possible error numbers from 0 to 1,000. The string constant UNUSED_ERROR_NUMBER is defined as
"Application-defined or object-defined error."
Microsoft uses this message to denote error numbers that aren't currently being used, but might be used at some later time. Rather than clutter the output with these empty errors, the code skips such empty errors and does not display them in the txtOutput text box. Any error number that does not contain the UNUSED_ERROR_NUMBER or an empty string is displayed in the text box. A sample run is shown in Figure 19.3.
Figure 19.3. Sample run of the debugger test program.
As the program runs, lblProgress shows which iteration of the loop is being processed . At completion, the label is used to show the count of runtime error messages that have been displayed in the text box. Figure 19.3 shows that there are (at the time of this writing) 89 runtime error messages. The first column of the output shows the error number (for example, 007 ) and its associated error message (for example, Out of memory ). I wonder if that relationship is by accident ?