Chapter 6: Debugging with Visual Studio

167

handling bad data and fixing null references. The program itself is not particularly sophisticated, but it contains just enough logic to lead you down a rat hole and show you how to work your way out. First, we’ll look at the program, and then we’ll follow up with two bug-fixing exercises.

A Program with Bugs

The code in this section contains bugs, and it’s important that you type it in as listed or use the downloadable code for this book from the McGraw-Hill Web site. I’ll describe each piece of code and try not to give away all of the secrets of the bugs just yet. Later, I’ll guide you through a process of discovery to find and fix the bugs. The program is a search application that takes the first name of a person and searches for that person through a list of customers. If the program finds the customer being searched for, it will print the customer’s first and last name. Otherwise, the program will print a message stating that it did not find the customer.

The program is divided into three major parts: a class to hold customer information, a class that will return a list of customers, and the class containing the Main method that runs the program. The following sections describe each of these classes.

The Customer Class

Any time you are working with data, you’ll have a class to hold that data. Since this application works with customers, the natural approach is to have a Customer class, as follows:

C#:

public class Customer

{

public string FirstName { get; set; } public string LastName { get; set; }

}

VB:

Public Class Customer

Property FirstName As String

Property LastName As String

End Class

This is the minimal information required for this demo, and any class that you build will have more properties. Notice that both properties are type string.

Page 190
Image 190
Microsoft 9GD00001 manual Program with Bugs, 167, Customer Class