1. Home
  2. Computing & Technology
  3. Visual Basic

Chapter 7 - Using Loops and Timers - Part 1

By Dan Mabbutt, About.com

6 of 8

Debug.WriteLine - A headstart on the next chapter

Debug.WriteLine

As you code the examples in the chapter and this lesson, here's a handy technique you can use to figure out exactly what's happening in your loops. We'll use the endless loop example that is found in the book to illustrate it:

~~~~~~~~~~~~~~~~~~~~~~~~~
Dim Number As Double
Do
  Number = InputBox( _
    "Enter a number to square. Type -1 to quit.")
  Number = Number * Number
  TextBox.Text = Number
Loop While Number >= 0
~~~~~~~~~~~~~~~~~~~~~~~~~

When you run this program, it's not obvious why it won't accept the -1 input to quit. Rather than just scratching your head about it, why not get more information to work with using Debug.WriteLine!! Try entering these statements into the loop:

~~~~~~~~~~~~~~~~~~~~~~~~~
Dim Number As Double
Do
  Number = InputBox( _
    "Enter a number to square. Type -1 to quit.")
  Debug.WriteLine("Number is: " & Number & " before square")
  Number = Number * Number
  TextBox.Text = Number
  Debug.WriteLine("Number is: " & Number & " before test")
Loop While Number >= 0
~~~~~~~~~~~~~~~~~~~~~~~~~

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Using Loops and Timers - Part 1

©2009 About.com, a part of The New York Times Company.

All rights reserved.