The Debug class

When debugging a program, it is sometimes handy to write intermediate output on the development system's Output debugging window. If the Output window is not visible, click View > Other Windows > Output.

Let us look at four methods of the debug class: WriteLine, Write, WriteLineIf, and WriteIf.

This example illustrates WriteLine:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer
   For a = 1 To 5
      Debug.WriteLine(a + 10)
   Next
End Sub

Would not display any output on the form, but it would write:

   11
   12
   13
   14
   15
in the development system's Debug window.

As you see, the program uses the WriteLine method of the Debug class. The arguments of the Writeline method can be a string, numeric or Boolean expression:

Debug.Writeline (<expression>)

The Debug class also has a Write method, which writes output in the Debug window but does not include a carriage-return line feed.

Finally, there is the WritelineIf and WriteIf methods. These methods only write output if a boolean expression is true. The syntax is:

Debug.WritelineIf (<Boolean expression>, <expression>)

For example, this program:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   Dim a As Integer
   For a = 1 To 5
      Debug.WriteLineIf (a = 2, a + 10)
   Next
End Sub

only writes output when the variable a has a value of 2.

Note that if you use the Debug class, be sure you compile in debug mode (as opposed to release mode).


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.