18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

THE V ISUAL BASIC 6 ENVIRONMENT<br />

perform some action when the user clicks the control. You can also add events<br />

that certain objects generate. VBA and Visual Basic let you declare a variable with<br />

the keyword WithEvents. WithEvents tells the development environment that the<br />

object variable will be used to respond to the object’s events. This is sometimes<br />

referred to as an “event sink”. The declaration must be made in a class module or<br />

a form. Here’s how you declare a variable and expose the events of an object in<br />

the Declarations section:<br />

Private WithEvents m_pViewEvents as Map<br />

Visual Basic only supports one outbound interface (marked as the default outbound<br />

interface in the IDL) per coclass. To get around this limitation, the<br />

coclasses that implement more than one outbound interface have an associated<br />

dummy coclass that allows access to the secondary outbound interface. These<br />

coclasses have the same name as the outbound interface they contain, minus the I.<br />

Private WithEvents m_pMapEvents as MapEvents<br />

Once you’ve declared the variable, search for its name in the Object combo box<br />

at the top left of the code window. Then, inspect the list of events to which you<br />

can attach code in the Procedure/Events combo box at the top right of the code<br />

window.<br />

Not all procedures of the outbound event interface need to be stubbed out, as<br />

Visual Basic will stub out any unimplemented methods. This is different from<br />

inbound interfaces, in which all methods must be stubbed out for compilation to<br />

occur.<br />

Before the methods are called, the hookup between the event source and sink<br />

must be made. This is done by setting the variable that represents the sink to the<br />

event source.<br />

Set m_pMapEvents = MapControl1.Map<br />

Pointers to valid objects as parameters<br />

Some <strong>ArcGIS</strong> methods expect interfaces for some of their parameters. The<br />

interface pointers passed can point to an instanced object before the method call<br />

or after the method call is completed.<br />

For example, if you have a polygon (pPolygon) whose center point you want to<br />

find, you can write code as follows:<br />

Dim pArea As IArea<br />

Dim pPt As IPoint<br />

Set pArea = pPolygon ' QI for IArea on pPolygon<br />

Set pPt = pArea.Center<br />

You don’t need to create pPt because the Center method creates a Point object for<br />

you and passes back a reference to the object via its IPoint interface. Only methods<br />

that use client-side storage require you to create the object prior to the<br />

method call.<br />

Passing data between modules<br />

When passing data between modules it is best to use accessor and mutator functions<br />

that manipulate some private member variable. This provides data encapsulation,<br />

which is a fundamental technique in object-oriented programming. Public<br />

variables should never be used.<br />

88 • <strong>ArcGIS</strong> <strong>Engine</strong> <strong>Developer</strong> <strong>Guide</strong>

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

Saved successfully!

Ooh no, something went wrong!