360Microsoft Visual Studio 2010: A Beginner’s Guide

Writing Macros

When the productivity features that ship with VS and custom snippets don’t give you enough power, the next step is to consider creating a macro, which is a repeatable set of actions that you can record and re-run multiple times. An example of when a macro is useful is whenever you find yourself continuously repeating the same set of actions in VS. This section will show you how to create and run a macro that uses VS features to create a customized block of code for validating strings.

Recording a Macro

When creating business objects, it’s common to validate input parameters to ensure they are valid. One such validation is enforcing that calling code pass a required parameter. The example in this section shows you how to write a macro for validating that a string- type parameter is not null, empty, or white space (such as a space or tab). To get started, create a new Console project and add a Class file with the following method to the project, which simulates adding a new customer:

C#:

using System;

class Customer

{

public int AddNewCustomer(string firstName, string lastName)

{

int newCustID = 0;

//Logic to add customer return newCustID;

}

}

VB:

Public Class Customer

Function AddNewCustomer(

ByVal firstName As String,

ByVal lastName As String) As Integer

Dim newCustID As Integer = 0

' Logic to add customer

Page 383
Image 383
Microsoft 9GD00001 manual Writing Macros, Recording a Macro