18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

THE V ISUAL BASIC 6 ENVIRONMENT<br />

Basic has hidden the interface-based programming to simplify the developer<br />

experience. This is not an issue if all the functionality implemented by the object<br />

can be accessed via one interface, but it is an issue when there are multiple interfaces<br />

on an object that provides services.<br />

The Visual Basic Editor backs this up by hiding default interfaces from the<br />

IntelliSense completion list and the object browser. By default, any interfaces that<br />

begin with an underscore, “_”, are not displayed in the object browser (to display<br />

these interfaces, turn Show Hidden Member on, although this will still not display<br />

default interfaces).<br />

You have already learned that the majority of ArcObjects have IUnknown as their<br />

default interface and that Visual Basic does not expose any of IUnknown’s methods,<br />

namely, QueryInterface, AddRef, and Release. Assume you have a class Foo that<br />

supports three interfaces, IUnknown (the default interface), IFoo, and IBar. This<br />

means that if you were to dimension the variable pFoo as below, the variable pFoo<br />

would point to the IUnknown interfaces.<br />

Dim pFoo As New Foo ' Create a new Foo object<br />

pFoo.??????<br />

Since Visual Basic does not allow direct access to the methods of IUnknown, you<br />

would immediately have to QI for an interface with methods on it that you can<br />

call. Because of this, the correct way to dimension a variable that will hold<br />

pointers to interfaces is as follows:<br />

Dim pFoo As IFoo ' Variable will hold pointer to IFoo interface.<br />

Set pFoo = New Foo ' Create Instance of Foo object and QI for IFoo.<br />

Now that you have a pointer to one of the object’s interfaces, it is an easy matter<br />

to request from the object any of its other interfaces.<br />

Dim pBar as IBar ' Dim variable to hold pointer to interface<br />

Set pBar = pFoo ' QI for IBar interface<br />

By convention, most classes have an interface with the same name as the class<br />

with an “I” prefix; this tends to be the interface most commonly used when<br />

working with the object. You are not restricted to which interface you request<br />

when instantiating an object; any supported interface can be requested; hence, the<br />

code below is valid.<br />

Dim pBar as IBar<br />

Set pBar = New Foo ' CoCreate Object<br />

Set pFoo = pBar ' QI for interface<br />

Objects control their own lifetime, which requires clients to call AddRef anytime<br />

an interface pointer is duplicated by assigning it to another variable and to call<br />

Release anytime the interface pointer is no longer required. Ensuring that there are<br />

a matching number of AddRefs and Releases is important, and fortunately, Visual<br />

Basic performs these calls automatically. This ensures that objects do not “leak”.<br />

Even when interface pointers are reused, Visual Basic will correctly call release on<br />

the old interface before assigning the new interface to the variable. The following<br />

code illustrates these concepts; note the reference count on the object at the<br />

various stages of code execution.<br />

Chapter 4 • <strong>Developer</strong> environments • 83

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!