Microsoft 9GD00001 manual Fixing the First Bug

Models: 9GD00001

1 449
Download 449 pages 12.58 Kb
Page 197
Image 197

174Microsoft Visual Studio 2010: A Beginner’s Guide

representation of the data where any characters that don’t have a readable representation appear as dots. You can see “.J.e.a.n.” on the first line of the third column. .NET characters are 16-bit Unicode, and the data for the character only fills the first byte, resulting in the second byte being set to 00, causing the dots between characters you see in the first column. If the data used another character set, such as Japanese Kanji, you would see data in both bytes of the character. The hex representation of this data in the second column is “00 4a 00 65 00 61 00 6e 00 20”. Looking at the Unicode representation, which you can find at http://unicode.org/, you’ll see that the hex and visual representation of the characters match.

You can see that I’ve highlighted the 00 20 at the end of the first line of the second column in Figure 6-16, which proves that Jean is followed by a Unicode space character. Knowing this information might help you share information with someone who is responsible for the data, letting them know that there are extraneous spaces in the data. Some computer or software systems might even use other types of characters, perhaps a proprietary delimiter for separating data, and accidentally save the data with the delimiter.

Fixing the First Bug

While you might have bad data and it might not be your fault, the prospect of fixing the problem by fixing the data source is often illusive, meaning that you need to apply a fix in your code. In this section, we’ll apply a fix. However, we’ll put a convoluted twist in the solution where we discover a new bug when fixing the first. The purpose is twofold: to illustrate the real-world fact that there are often multiple problems with a given piece of code and to show a completely different type of bug that you will encounter when writing your own code. The following steps lead you through the fix and subsequent discovery of the new bug:

1.Press SHIFT-F5to stop the previous debugging session.

2.Implement a fix by commenting out the contents of the foreach loop and replacing with code that protects against extraneous spaces in the data, as follows:

C#:

var firstName = cust.FirstName.Trim(); var lastName = cust.LastName.Trim();

if (searchName == cust.FirstName)

Page 197
Image 197
Microsoft 9GD00001 manual Fixing the First Bug