Chapter 7 Quick Reference


Chapter 7 Quick Reference

To

Do this

Execute a group of program statements a specific number of times

Insert the statements between For and Next statements in a loop. For example:

Dim i As Integer
For I = 1 To 10
     MsgBox("Press OK already!")
Next i

Use a specific sequence of numbers with statements

Insert the statements in a For…Next loop, and use the To and Step keywords to define the sequence of numbers. For example:

Dim i As Integer
For i = 2 To 8 Step 2
    TextBox1.Text = TextBox1.Text & i
Next I

Avoid an endless Do loop

Be sure the loop has a test condition that can evaluate to False.

Exit a For…Next loop prematurely

Use the Exit For statement. For example:

Dim InpName As String 
Dim i As Integer 
For i = 1 To 10 
    InpName = InputBox("Name?") 
    If InpName = "Trotsky" Then Exit For 
    TextBox1.Text = InpName 
Next i

Execute a group of program statements until a specific condition is met

Insert the statements between Do and Loop statements. For example:

Dim Query As String = "" 
Do While Query <> "Yes" 
    Query = InputBox("Trotsky?") 
    If Query = "Yes" Then MsgBox("Hi")
Loop

Loop until a specific condition is True

Use a Do loop with the Until keyword. For example:

Dim GiveIn As String 
Do 
    GiveIn = InputBox("Say 'Uncle'")
Loop Until GiveIn = "Uncle"

Loop for a specific period of time in your program

Use the Timer control.

Insert a code snippet into your program

In the Code Editor, position the insertion point (I-beam) at the location where you want to insert the snippet. On the Edit menu, click Intelli-Sense, and then click Insert Snippet. Browse to the snippet that you want to use, and then double click the snippet name.

Add or reorganize snippets in the Insert Snippet list box

Click the Code Snippet Manager command on the Tools menu.


Chapter 8

Debugging Visual Basic Programs

After completing this chapter, you will be able to:

  • Identify different types of errors in your programs.

  • Use Microsoft Visual Studio debugging tools to set breakpoints and correct mistakes.

  • Use the Autos and Watch windows to examine variables during program execution.

  • Use a visualizer to examine string data types and complex data types within the IDE.

  • Use the Immediate and Command windows to change the value of variables and execute commands in Visual Studio.

In the past few chapters, you've had plenty of opportunity to make programming mistakes in your code. Unlike human conversation, which usually works well despite occasional grammatical mistakes and mispronunciations, communication between a software developer and the Microsoft Visual Basic compiler is successful only when the precise rules and regulations of the Visual Basic programming language are followed.

In this chapter, you'll learn more about the software defects, or bugs, that stop Visual Basic programs from running. You'll learn about the different types of errors that turn up in programs and how to use the Visual Studio debugging tools to detect and correct these defects. What you learn will be useful as you experiment with the programs in this book and when you write longer programs in the future.

Why focus on debugging now? Some programming books skip this topic altogether or place it near the end of the book (after you've learned all the language features of a particular product). There is a certain logic to postponing the discussion, but I think it makes the most sense to master debugging techniques while you learn to program so that detecting and correcting errors becomes part of your standard approach to writing programs and solving problems. At this point in Microsoft Visual Basic 2005 Step by Step, you know just enough about objects, decision structures, and statement syntax to create interesting programs but also enough to get yourself into a little bit of trouble! As you'll soon see, however, Microsoft Visual Studio 2005 makes it easy to uncover your mistakes and get back on the straight and narrow.

Upgrade Notes: Migrating Visual Basic 6 Code to Visual Basic 2005

If you're experienced with Microsoft Visual Basic 6, you'll notice some new features in Microsoft Visual Basic 2005, including the following:

  • Visual Studio includes several new tools for finding and correcting errors. A few of the familiar Visual Basic 6 debugging commands are still a part of Visual Studio (Break All, Step Into, Step Over), but there are also new debugging tools and commands, including revised Standard and Debug toolbars and a menu command that manages exceptions.

  • Several new debugging windows have been added to the Visual Studio IDE, including Autos, Command, Call Stack, Threads, Memory, Script Explorer, Disassembly, and Registers. You won't use these tools for every debugging session, but you might find them useful in more sophisticated applications.

  • In Visual Studio 2005, you can use a new visualizer feature (a magnifying glass icon) to examine the content of complex variables and objects within the IDE, so you can now view HTML, XML, and sophisticated datasets during a debugging session.