Microsoft 9GD00001 Computer Accessories User Manual


 
172 Microsoft Visual Studio 2010: A Beginner’s Guide
4. Next, set a breakpoint on the if statement, right-click the breakpoint, and set the
condition as follows:
C#:
cust.FirstName == "Jean"
VB:
cust.FirstName = "Jean"
The goal here is to see what happens when the if statement finds the record matching
the searchName. At this point, we’re assuming that Jean does exist in the data. Working
with a small program, you can use windows such as Autos, Locals, or Watch to find
this record. However, many real-world scenarios will give you a list with many more
records. Therefore, rather than waste time drilling down through dozens of records, use
the VS debugger to help find the record quickly. Keep in mind that all the best plans
don’t always work out, as you’ll soon see, but the primary point is taking the most
productive step first. Setting a conditional breakpoint demonstrates how you can set
conditions that can avoid eating up time caused by stepping through loops.
5. Press F5 to run the program. You expect to hit the breakpoint, but that won’t happen.
Confusing? We know that there isn’t anything wrong with the logic, because the if
statement condition is a simple equality operator. Perhaps we’ve looked in the database
or whatever source the data came from, but it’s given in this scenario that Jean is
definitely in the data. However, this illustrates a common problem where the quality of
data you work with is less than desired.
6. This time, change the breakpoint condition on the if statement as follows and re-run the
program:
C#:
cust.FirstName.Contains("Jean")
VB:
cust.FirstName.Contains("Jean")
Remember, we suspect bad data, so the call to Contains on the string assumes that there
might be some extraneous white space or other characters around the name in the data.
Hover over cust.FirstName or look at cust in one of the debug windows to verify it is
the record you are looking for. This breakpoint will pause on any records that contain
the sequence of characters “Jean”, such as Jean-Claude. So, you might have multiple
matches that aren’t what you want. The benefit is that the number of records you must