Chapter 6: Debugging with Visual Studio | 169 |
{
.FirstName = "Jean "
},
New Customer With
{
.FirstName = "Wim",
.LastName = "Meister"
}
}
Return customers
End Function
End Class
The GetCustomers method returns a List<Customer> (List(Of Customer) in VB). For the purposes of this discussion, how the GetCustomers method works won’t matter. Such a method could easily get customers from a database, Web service, or other object. For simplicity, GetCustomers initializes a List with Customer objects. The part of this method that is particularly important is the customer whose FirstName property is set to “Jean ”. Notice the blank space appended to the name, which is required to make this scenario behave as designed (i.e., to intentionally create a bug). It’s also conspicuous that the Customer object with a FirstName property set to “Jean ” also does not have a LastName.
The Program with Bugs
The following is a search program that uses CustomerRepository to get a list of Customer objects. The logic will iterate through the results, checking to see if the result is equal to the search term. When the result is equal, the program prints the full name of the customer. If no matching customers are found, the program indicates that the customer wasn’t found:
C#:
using System;
class Program
{
static void Main()
{
var custRep = new CustomerRepository();
var customers = custRep.GetCustomers();
var searchName = "Jean"; bool customerFound = false;