
362Microsoft Visual Studio 2010: A Beginner’s Guide
could end up saving bad data. Since AddNewCustomer doesn’t have an implementation, this is all speculation, but this outlines a few of the many problems that can occur if you allow your business objects to accept data that is bad for your program.
The macro demonstrated in this section will show how to check a string parameter for null, empty, or white space and throw an ArgumentNullException. This will prevent callers from passing bad data and give them a meaningful message. To create a macro, you will need to locate the position in the code where the macro starts (if applicable), start recording, perform VS actions, and stop recording. It’s somewhat like using a video recorder where you have to find a TV show, start the recording, allow the show to play, and then stop recording. Perform the following steps to create the parameter validation macro:
1.Click the firstName parameter of the AddNewCustomer method so that the cursor is inside of the firstName parameter identifier. This is important because we need the parameter name in the code.
2.Start the macro recorder by selecting Tools Macros Record TemporaryMacro or press
3.For C#, press
the parameter name.
4.For C#, press
5.Type if and press TAB twice (the if snippet), type string.IsNullOrWhiteSpace( into the condition, press
6.Type throw new ArgumentNullException(", press
7.Press DOWN ARROW and press ENTER. This positions the cursor after the code, which might be convenient if you want to continue typing from this point.
8.Select Tools Macros Stop Recording TemporaryMacro or press