Microsoft 9GD00001 Computer Accessories User Manual


 
364 Microsoft Visual Studio 2010: A Beginner’s Guide
In the preceding code, I’ve moved the ArgumentNullException arguments to separate
lines to fit the book’s line length, but this is what you should see. Next, you can test
the macro by running it. Click the lastName parameter and select Tools | Macros | Run
TemporaryMacro or press
CTRL-SHIFT-P. That will produce the following code:
public int AddNewCustomer(string firstName, string lastName)
{
if (string.IsNullOrWhiteSpace(lastName))
{
throw new ArgumentException("lastName", "lastName value is not
valid.");
}
if (string.IsNullOrWhiteSpace(firstName))
{
throw new ArgumentException("firstName", "firstName value is
not valid.");
}
int newCustID = 0;
// Logic to add customer
return newCustID;
}
Now, you can run this macro on any of the string parameters of methods in your
class and quickly add validation support. The only problem at the present time is that
the macro is overwritten as soon as you begin recording a new macro and the macro is
gone if you close VS. The next section addresses this problem by showing you how to
save the macro.
Saving a Macro
You can save macros to be reused in later sessions. To save the macro, select Tools |
Macros | Save TemporaryMacro. VS will save TemporaryMacro and open the Macro
Explorer window, shown in Figure 12-8.
VS uses TemporaryMacro as the name of whatever macro it will record. Therefore,
you must rename the macro if you want to keep it because the next recording will
overwrite this macro. Rename the file macro to ValidateStringParameter by right-
clicking TemporaryMacro in Macro Explorer, showing the context menu, and selecting
Rename.