384Microsoft Visual Studio 2010: A Beginner’s Guide

Reviewing the OnConnection Method

As you learned earlier, the Connect class implements various interface methods so that VS can call into those methods to run your Add-In. One of the primary methods is OnConnection, which is a member of the IDTExtensibility2 interface. VS calls OnConnection when the Add-In loads. When calling OnConnection, VS passes four parameters that you can use to initialize the Add-In. The Add-In Project Wizard, covered in a previous section of this chapter, generates much skeleton code that uses parameter values in OnConnection to initialize the Add-In. While the example in this chapter doesn’t modify the OnConnection method, understanding the code is helpful in learning how the Add-In initializes and how it does affect the code you will write later. We’ll first take another look at OnConnection parameters and then examine the generated code.

Understanding OnConnection Parameters

The OnConnection method has four parameters. Each of the parameters are passed to the OnConnection method by VS; these parameters provide all of the information necessary for initializing the Add-In. Table 13-3 lists each parameter and its purpose.

Member

Type

Purpose

application

Compile-time type is Object, but

Application is the parent object for the entire VS

 

the runtime type is defined by the

automation model. You use this to access all of the

 

version you’re at. For example,

windows, commands, and other parts of the IDE.

 

on older versions of VS, the

 

 

runtime type of Application was

 

 

DTE, but the runtime type of

 

 

Application in VS 2010 is DTE2.

 

connectMode

Enum of type ext_ConnectMode

Read this parameter to figure out when and how

 

 

the Add-In was loaded. In a following section,

 

 

you’ll see how the OnConnection method reads this

 

 

value to figure out when the Add-In loads for the

 

 

first time.

addInInst

The compile-time type is Object,

This refers to the Add-In itself, allowing you to

 

but runtime type is AddIn.

inspect various properties of the Add-In.

custom

Array

These aren’t used in the current example, but

 

 

consider the fact that we’re implementing an

 

 

interface. Besides VS 2010, you could have

 

 

another application (host) that supported Add-Ins

 

 

that implement the IDTExtensibility2 interface. Those

 

 

hosts could use the custom array parameter to pass

 

 

information specific to that application. Therefore,

 

 

custom is another extensibility point to make the

 

 

IDTExtensibility2 interface more flexible.

 

 

 

Table 13-3 OnConnection Method Parameters

Page 407
Image 407
Microsoft 9GD00001 manual Reviewing the OnConnection Method, Understanding OnConnection Parameters