Chapter 12: Customizing the Development Environment

369

work on the file you ran it in. This problem doesn’t occur in the macro for VB. I’ll show you how to fix this problem, but let’s open the macro editor first.

You can open the Macro editor through VS by selecting Tools Macros Macros IDE, start a new project, add a module to the project, and add a Sub to the Module as a new macro. Then code the macro by typing DTE. and using Intellisense to find various parts of the IDE. The cryptic parameter to Windows.Item, {CF2DDC32-8CAD-11D2-9302- 005345000000}, for the Find And Replace window is called a Globally Unique Identifier (GUID). A GUID is often used as a special identifier for software components and is the method used in VS to uniquely identify tools. So, DTE.Windows.Item("{CF2DDC32- 8CAD-11D2-9302-005345000000}").Activate()is a way to reference and open the Find And Replace window.

There is a problem with the macro for C# in Listing 12-3, because it will only work in the Customer.cs file in VS. The VB code below is provided for your convenience, but this problem only occurs with the macro written for C# code; the VB macro works fine on the VB code below. If you created a new class named Product in a file named Product.cs and added an AddNewProduct method like the following, the macro will try to open and write into the Customer.cs file, which is not the result you want:

C#:

using System;

namespace ConsoleApplication1

{

class Product

{

public int AddNewProduct(string productName)

{

int newProdID = 0;

//Logic to add product return newProdID;

}

}

}

VB (doesn’t have problem that occurs in C# code):

Public Class Product

Function AddNewProduct(ByVal productName As String) As Integer

Dim newProdID As Integer = 0

Page 392
Image 392
Microsoft 9GD00001 manual 369, VB doesn’t have problem that occurs in C# code