- •Using Your Sybex Electronic Book
- •Acknowledgments
- •Contents at a Glance
- •Introduction
- •Who Should Read This Book?
- •How About the Advanced Topics?
- •The Structure of the Book
- •How to Reach the Author
- •The Integrated Development Environment
- •The Start Page
- •Project Types
- •Your First VB Application
- •Making the Application More Robust
- •Making the Application More User-Friendly
- •The IDE Components
- •The IDE Menu
- •The Toolbox Window
- •The Solution Explorer
- •The Properties Window
- •The Output Window
- •The Command Window
- •The Task List Window
- •Environment Options
- •A Few Common Properties
- •A Few Common Events
- •A Few Common Methods
- •Building a Console Application
- •Summary
- •Building a Loan Calculator
- •How the Loan Application Works
- •Designing the User Interface
- •Programming the Loan Application
- •Validating the Data
- •Building a Math Calculator
- •Designing the User Interface
- •Programming the MathCalculator App
- •Adding More Features
- •Exception Handling
- •Taking the LoanCalculator to the Web
- •Working with Multiple Forms
- •Working with Multiple Projects
- •Executable Files
- •Distributing an Application
- •VB.NET at Work: Creating a Windows Installer
- •Finishing the Windows Installer
- •Running the Windows Installer
- •Verifying the Installation
- •Summary
- •Variables
- •Declaring Variables
- •Types of Variables
- •Converting Variable Types
- •User-Defined Data Types
- •Examining Variable Types
- •Why Declare Variables?
- •A Variable’s Scope
- •The Lifetime of a Variable
- •Constants
- •Arrays
- •Declaring Arrays
- •Initializing Arrays
- •Array Limits
- •Multidimensional Arrays
- •Dynamic Arrays
- •Arrays of Arrays
- •Variables as Objects
- •So, What’s an Object?
- •Formatting Numbers
- •Formatting Dates
- •Flow-Control Statements
- •Test Structures
- •Loop Structures
- •Nested Control Structures
- •The Exit Statement
- •Summary
- •Modular Coding
- •Subroutines
- •Functions
- •Arguments
- •Argument-Passing Mechanisms
- •Event-Handler Arguments
- •Passing an Unknown Number of Arguments
- •Named Arguments
- •More Types of Function Return Values
- •Overloading Functions
- •Summary
- •The Appearance of Forms
- •Properties of the Form Control
- •Placing Controls on Forms
- •Setting the TabOrder
- •VB.NET at Work: The Contacts Project
- •Anchoring and Docking
- •Loading and Showing Forms
- •The Startup Form
- •Controlling One Form from within Another
- •Forms vs. Dialog Boxes
- •VB.NET at Work: The MultipleForms Project
- •Designing Menus
- •The Menu Editor
- •Manipulating Menus at Runtime
- •Building Dynamic Forms at Runtime
- •The Form.Controls Collection
- •VB.NET at Work: The DynamicForm Project
- •Creating Event Handlers at Runtime
- •Summary
- •The TextBox Control
- •Basic Properties
- •Text-Manipulation Properties
- •Text-Selection Properties
- •Text-Selection Methods
- •Undoing Edits
- •VB.NET at Work: The TextPad Project
- •Capturing Keystrokes
- •The ListBox, CheckedListBox, and ComboBox Controls
- •Basic Properties
- •The Items Collection
- •VB.NET at Work: The ListDemo Project
- •Searching
- •The ComboBox Control
- •The ScrollBar and TrackBar Controls
- •The ScrollBar Control
- •The TrackBar Control
- •Summary
- •The Common Dialog Controls
- •Using the Common Dialog Controls
- •The Color Dialog Box
- •The Font Dialog Box
- •The Open and Save As Dialog Boxes
- •The Print Dialog Box
- •The RichTextBox Control
- •The RTF Language
- •Methods
- •Advanced Editing Features
- •Cutting and Pasting
- •Searching in a RichTextBox Control
- •Formatting URLs
- •VB.NET at Work: The RTFPad Project
- •Summary
- •What Is a Class?
- •Building the Minimal Class
- •Adding Code to the Minimal Class
- •Property Procedures
- •Customizing Default Members
- •Custom Enumerations
- •Using the SimpleClass in Other Projects
- •Firing Events
- •Shared Properties
- •Parsing a Filename String
- •Reusing the StringTools Class
- •Encapsulation and Abstraction
- •Inheritance
- •Inheriting Existing Classes
- •Polymorphism
- •The Shape Class
- •Object Constructors and Destructors
- •Instance and Shared Methods
- •Who Can Inherit What?
- •Parent Class Keywords
- •Derived Class Keyword
- •Parent Class Member Keywords
- •Derived Class Member Keyword
- •MyBase and MyClass
- •Summary
- •On Designing Windows Controls
- •Enhancing Existing Controls
- •Building the FocusedTextBox Control
- •Building Compound Controls
- •VB.NET at Work: The ColorEdit Control
- •VB.NET at Work: The Label3D Control
- •Raising Events
- •Using the Custom Control in Other Projects
- •VB.NET at Work: The Alarm Control
- •Designing Irregularly Shaped Controls
- •Designing Owner-Drawn Menus
- •Designing Owner-Drawn ListBox Controls
- •Using ActiveX Controls
- •Summary
- •Programming Word
- •Objects That Represent Text
- •The Documents Collection and the Document Object
- •Spell-Checking Documents
- •Programming Excel
- •The Worksheets Collection and the Worksheet Object
- •The Range Object
- •Using Excel as a Math Parser
- •Programming Outlook
- •Retrieving Information
- •Recursive Scanning of the Contacts Folder
- •Summary
- •Advanced Array Topics
- •Sorting Arrays
- •Searching Arrays
- •Other Array Operations
- •Array Limitations
- •The ArrayList Collection
- •Creating an ArrayList
- •Adding and Removing Items
- •The HashTable Collection
- •VB.NET at Work: The WordFrequencies Project
- •The SortedList Class
- •The IEnumerator and IComparer Interfaces
- •Enumerating Collections
- •Custom Sorting
- •Custom Sorting of a SortedList
- •The Serialization Class
- •Serializing Individual Objects
- •Serializing a Collection
- •Deserializing Objects
- •Summary
- •Handling Strings and Characters
- •The Char Class
- •The String Class
- •The StringBuilder Class
- •VB.NET at Work: The StringReversal Project
- •VB.NET at Work: The CountWords Project
- •Handling Dates
- •The DateTime Class
- •The TimeSpan Class
- •VB.NET at Work: Timing Operations
- •Summary
- •Accessing Folders and Files
- •The Directory Class
- •The File Class
- •The DirectoryInfo Class
- •The FileInfo Class
- •The Path Class
- •VB.NET at Work: The CustomExplorer Project
- •Accessing Files
- •The FileStream Object
- •The StreamWriter Object
- •The StreamReader Object
- •Sending Data to a File
- •The BinaryWriter Object
- •The BinaryReader Object
- •VB.NET at Work: The RecordSave Project
- •The FileSystemWatcher Component
- •Properties
- •Events
- •VB.NET at Work: The FileSystemWatcher Project
- •Summary
- •Displaying Images
- •The Image Object
- •Exchanging Images through the Clipboard
- •Drawing with GDI+
- •The Basic Drawing Objects
- •Drawing Shapes
- •Drawing Methods
- •Gradients
- •Coordinate Transformations
- •Specifying Transformations
- •VB.NET at Work: Plotting Functions
- •Bitmaps
- •Specifying Colors
- •Defining Colors
- •Processing Bitmaps
- •Summary
- •The Printing Objects
- •PrintDocument
- •PrintDialog
- •PageSetupDialog
- •PrintPreviewDialog
- •PrintPreviewControl
- •Printer and Page Properties
- •Page Geometry
- •Printing Examples
- •Printing Tabular Data
- •Printing Plain Text
- •Printing Bitmaps
- •Using the PrintPreviewControl
- •Summary
- •Examining the Advanced Controls
- •How Tree Structures Work
- •The ImageList Control
- •The TreeView Control
- •Adding New Items at Design Time
- •Adding New Items at Runtime
- •Assigning Images to Nodes
- •Scanning the TreeView Control
- •The ListView Control
- •The Columns Collection
- •The ListItem Object
- •The Items Collection
- •The SubItems Collection
- •Summary
- •Types of Errors
- •Design-Time Errors
- •Runtime Errors
- •Logic Errors
- •Exceptions and Structured Exception Handling
- •Studying an Exception
- •Getting a Handle on this Exception
- •Finally (!)
- •Customizing Exception Handling
- •Throwing Your Own Exceptions
- •Debugging
- •Breakpoints
- •Stepping Through
- •The Local and Watch Windows
- •Summary
- •Basic Concepts
- •Recursion in Real Life
- •A Simple Example
- •Recursion by Mistake
- •Scanning Folders Recursively
- •Describing a Recursive Procedure
- •Translating the Description to Code
- •The Stack Mechanism
- •Stack Defined
- •Recursive Programming and the Stack
- •Passing Arguments through the Stack
- •Special Issues in Recursive Programming
- •Knowing When to Use Recursive Programming
- •Summary
- •MDI Applications: The Basics
- •Building an MDI Application
- •Built-In Capabilities of MDI Applications
- •Accessing Child Forms
- •Ending an MDI Application
- •A Scrollable PictureBox
- •Summary
- •What Is a Database?
- •Relational Databases
- •Exploring the Northwind Database
- •Exploring the Pubs Database
- •Understanding Relations
- •The Server Explorer
- •Working with Tables
- •Relationships, Indices, and Constraints
- •Structured Query Language
- •Executing SQL Statements
- •Selection Queries
- •Calculated Fields
- •SQL Joins
- •Action Queries
- •The Query Builder
- •The Query Builder Interface
- •SQL at Work: Calculating Sums
- •SQL at Work: Counting Rows
- •Limiting the Selection
- •Parameterized Queries
- •Calculated Columns
- •Specifying Left, Right, and Inner Joins
- •Stored Procedures
- •Summary
- •How About XML?
- •Creating a DataSet
- •The DataGrid Control
- •Data Binding
- •VB.NET at Work: The ViewEditCustomers Project
- •Binding Complex Controls
- •Programming the DataAdapter Object
- •The Command Objects
- •The Command and DataReader Objects
- •VB.NET at Work: The DataReader Project
- •VB.NET at Work: The StoredProcedure Project
- •Summary
- •The Structure of a DataSet
- •Navigating the Tables of a DataSet
- •Updating DataSets
- •The DataForm Wizard
- •Handling Identity Fields
- •Transactions
- •Performing Update Operations
- •Updating Tables Manually
- •Building and Using Custom DataSets
- •Summary
- •An HTML Primer
- •HTML Code Elements
- •Server-Client Interaction
- •The Structure of HTML Documents
- •URLs and Hyperlinks
- •The Basic HTML Tags
- •Inserting Graphics
- •Tables
- •Forms and Controls
- •Processing Requests on the Server
- •Building a Web Application
- •Interacting with a Web Application
- •Maintaining State
- •The Web Controls
- •The ASP.NET Objects
- •The Page Object
- •The Response Object
- •The Request Object
- •The Server Object
- •Using Cookies
- •Handling Multiple Forms in Web Applications
- •Summary
- •The Data-Bound Web Controls
- •Simple Data Binding
- •Binding to DataSets
- •Is It a Grid, or a Table?
- •Getting Orders on the Web
- •The Forms of the ProductSearch Application
- •Paging Large DataSets
- •Customizing the Appearance of the DataGrid Control
- •Programming the Select Button
- •Summary
- •How to Serve the Web
- •Building a Web Service
- •Consuming the Web Service
- •Maintaining State in Web Services
- •A Data-Driven Web Service
- •Consuming the Products Web Service in VB
- •Summary
DEBUGGING 803
This code is taken from a ProgressBar control. It is the code that implements the Value property of the ProgressBar control. A check is done to make sure that the value that the property is set to is less than or equal to the value of the Max property, since you can’t set the current value to be bigger than the maximum defined value. If the property is trying to be set to a value larger than the max, then an exception is generated via the Throw statement. This statement instantiates an exception of class OverflowException and produces a custom error that the fellow developer can see in his own exception handler.
Debugging
As you’ve seen, encountering errors is nearly a certainty when developing a piece of software. Syntax errors, of course, are the easiest to detect, because the IDE tells you right where they are and what the nature of the error is. It’s the runtime errors that are harder to locate and correct, because of the many forms these errors can take. You’ve seen examples of errors that will cause your program to crash, as well as errors that spiral your program off into an infinite loop, and even errors that produce no outward signs at all—they simply cause the program to behave in some unintended way.
Fortunately, Visual Studio .NET provides you with a fine selection of tools to detect and remove the errors in your program. The act of hunting and eliminating errors is called debugging, because you’re goal is to remove the bugs (or de-bug) the program.
Breakpoints
The breakpoint is the first and most important weapon in the war against bugs. When you set a breakpoint in your program, you’re telling VS.NET to stop execution of the program when it reaches a certain line in the code. Once stopped, you can examine the state of the program, including the values of the variables, the procedure stack, and the contents of memory.
Before we can look at debugging essentials, we need some buggy code. Let’s write a program to count all the vowels in a string. To set this program up, start a new WinForms project, then add a button named cbCount and a TextBox named tbPhrase to the form. Add the code from Listing 17.9 to the project.
Listing 17.9: Bug-Filled Code
Private Sub cbCount_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cbCount.Click cbCount.Text = CountTheVowels(cbCount.Text)
End Sub
Private Function CountTheVowels(ByVal cSomeString As String) As Integer Dim x As Integer = 1
Dim iTot As Integer = 0 Dim iPos As Integer
Do While x <= cSomeString.Length
iPos = InStr(“aeio”, cSomeString.Substring(x, 1).ToLower) If iPos > 0 Then
iTot += 1
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
804 Chapter 17 ERROR HANDLING AND DEBUGGING
End If
Loop
Return iTot
End Function
The button click event passes the contents of the text box into the function CountTheVowels(), which is where all the dirty work will be performed. When the count is obtained, the caption of the button should be replaced with the vowel count. Once you get the code for the program typed in exactly as seen above, try running the program, entering some text into the text box, and clicking the button. Then wait. And wait. My guess is that the caption of the button will not change until you stop the application by pressing Ctrl+Break (or select Debug Stop Debugging). If you wait long enough, an overflow exception will occur. This means that the value of the variable iTot has exceeded the maximum value you can represent with an Integer.
Obviously, this little function shouldn’t take very long to run, so something screwy must be going on, like an infinite loop. Let’s set a breakpoint in the function and see if we can spot it.
To set a breakpoint, place the cursor on a line of code in the function where you want the program to stop, and press the F9 key. The line of code should become highlighted in red, as seen in Figure 17.9.
Figure 17.9
Setting a breakpoint
Once a breakpoint is set, you can begin the program, type some text into the text box, and click the button. Like a good soldier, the debugger should come up on that same line of code, this time highlighted in yellow. This means that the program has stopped execution on that exact line of code.
Now we can start looking around. First, take the cursor and hover it over some of the areas of code. You should be able to see a tooltip displaying the value of the various variables you rest the mouse over, like the one in Figure 17.10.
Figure 17.10
Tooltips display the value of variables.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
DEBUGGING 805
The figure displays the first of the errors I’ve made coding this program. The value of the variable cSomeString is “Count Vowels”, but this is not the string I typed into my text box. Why is this string being passed into the function? A quick examination of the function call reveals this problem.
cbCount.Text = CountTheVowels(cbCount.Text)
Note that I inadvertently passed the Text property of the button cbCount, when my intention was to pass in the value of tbPhrase.Text. This is a perfect example of a logic error. The code works fine (well, anyway, it will work fine once we find the rest of these bugs), but it won’t count the vowels in the string that we intended to count. The fix for this first bug is easy. First, stop the program from running by selecting Stop Debugging from the Debug menu (shortcut key is Shift+F5).
Note Spend some time memorizing the shortcut keys for all the debugging functions. You’ll be make using these functions quite a bit, and use of the shortcut keys will save a ton of time.
Once the program is stopped, change the CountTheVowels() function call as follows (note the change marked in bold font). Now we’re passing in the string we intended.
cbCount.Text = CountTheVowels(tbPhrase.Text)
Stepping Through
As it often happens, we started looking for an infinite loop but found another, unrelated bug first. Now that we’ve squashed that bug, we can go back to running the program and looking for the original problem. Start up the program again, type some text into the text box, and click the button. Once again, the program should stop at the breakpoint.
Let’s watch a few of the program lines run in sequence and see if that tells us anything. To make the program step through the current line of code, press the F10 key. Each time you press F10, one line of code will execute, and the yellow highlight will move to the next line of code that is about to be executed.
Note The F11 key also steps through the code, but it will step into any procedures that are called. The F10 key steps over the procedure calls, running them all at once and returning back to the original spot. This allows you to skip over the line-by-line tracing of procedures that you are not currently debugging.
You can continue to trace through the loop line by line and examine variable values with the tooltips. Can you figure out the cause of the infinite loop? Perhaps it’s time to bring in some more debugging tools.
The Local and Watch Windows
While still stopped in debug mode, select Debug Windows Locals from the menu. The Locals window (Figure 17.11) should be displayed in the lower section of the IDE. This window shows you the current value of all of the locally declared variables. Now we can see the value of the variables changing as we step through the program.
Try stepping through the loop a few more times. What you might notice is that the values of the variables aren’t changing. To get even more information, highlight the entire phrase cSomeString
.Substring(x, 1).ToLower, right-click, and select Add Watch from the context menu. This will bring up the Watch window, as seen in Figure 17.12.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
806 Chapter 17 ERROR HANDLING AND DEBUGGING
Figure 17.11
The Locals window
Figure 17.12
The Watch window
The Watch window is similar to the Locals window, but it allows you to look at the value of complex expressions like the one we just placed in it. Once again, try stepping through the loop a few times. You might expect that the Substring command would be incrementing the letter in the string as the loop iterates, but that isn’t happening. The only logical reason for this is that the value of counter variable x isn’t changing. Let’s look at the loop again.
Do While x <= cSomeString.Length
iPos = InStr(“aeio”, cSomeString.Substring(x, 1).ToLower) If iPos > 0 Then
iTot += 1 End If
Loop
I’ve made one of the classic looping blunders here. I set up a counter variable x to loop through the string character by character, but I never added any code to increment the string! That’s as sure a recipe for an infinite loop as anything. Fixing that problem is an easy remedy (see the code highlighted in bold).
Do While x <= cSomeString.Length
iPos = InStr(“aeio”, cSomeString.Substring(x, 1).ToLower) If iPos > 0 Then
iTot += 1 End If
x += 1
Loop
Okay, that bug is squashed, so it’s time to remove the breakpoint and rerun the program to see if it works. This time, however, the program crashes and burns with the following error.
An unhandled exception of type ‘System.ArgumentOutOfRangeException’ occurred in mscorlib.dll. Additional information: Index and length must refer to a location within the string.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
DEBUGGING 807
We’ve seen that error before—it means that we tried to look at a character beyond the length of the string. By checking the Locals window, you should be able to eventually track down this problem. The problem here is that the loop counter x starts at character 1 and ends at value cSomeString
.Length. While that range is correct for VB version 6 and below, .NET strings are indexed starting at 0. Oops. We need to modify our procedure as shown by the two bold items here, the zero and the lessthan sign:
Private Function CountTheVowels(ByVal cSomeString As String) As Integer Dim x As Integer = 0
Dim iTot As Integer = 0 Dim iPos As Integer
Do While x < cSomeString.Length
iPos = InStr(“aeio”, cSomeString.Substring(x, 1).ToLower) If iPos > 0 Then
iTot += 1 End If
x += 1 Loop Return iTot
End Function
This modified loop starts at 0 and ends at cSomeString.Length - 1, which is the correct way to iterate through a .NET string. Once again, you can try to remove all breakpoints and rerun the application. This time, it should actually produce a value, like the successful test in Figure 17.13.
Figure 17.13
We’ve debugged our program … or have we?
Finally, we got an answer! But is it the correct answer? A manual count gives 11 vowels in the test string “The quick brown fox jumps over the lazy dog”. Since this is a fairly long test string, you might want to try a smaller string, like the string “aeiou” shown in Figure 17.14.
Now that’s definitely wrong—it’s counted only four of the five vowels in the string. Looks like there’s another logic error. Back to the debugging drawing board … Actually, this error is pretty easy compared with some of the others we’ve already squashed. Look at the actual comparison of the character to the vowel list.
iPos = InStr(“aeio”, cSomeString.Substring(x, 1).ToLower)
Where’s the u? It looks like I simply forgot to include the u in the vowel list. After adding the u back in, the final, working code looks like Listing 17.10.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
808 Chapter 17 ERROR HANDLING AND DEBUGGING
Figure 17.14
It’s clear that a mistake occurred.
Listing 17.10: Bug-Free Code
Private Sub cbCount_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cbCount.Click cbCount.Text = CountTheVowels(tbPhrase.Text)
End Sub
Private Function CountTheVowels(ByVal cSomeString As String) As Integer Dim x As Integer = 0
Dim iTot As Integer = 0 Dim iPos As Integer
Do While x < cSomeString.Length
iPos = InStr(“aeiou”, cSomeString.Substring(x, 1).ToLower) If iPos > 0 Then
iTot += 1 End If
x += 1 Loop Return iTot
End Function
This is the type of error you can only catch with exhaustive tests—that is, only a user will likely catch this error. You can actually test the program with a string that doesn’t contain the character “u,” see that it works nicely, and distribute it. Very soon you will receive messages to the effect that your application doesn’t work. Yet this application has been tested and seems to work fine. The tests, however, were not exhaustive.
You should also try to test your applications with extreme situations (a blank string, for example, or a very large one, an invalid numeric value, and so on). The final test, of course, is to pass the applications to users and ask for their comments. Unfortunately, we don’t write software for each other. We write software for people knowledgeable enough to crash an application in minutes but not knowledgeable enough to keep it running.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |