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.

DEVELOPING WITH A RCOBJECTS<br />

Accessing collection elements<br />

When accessing elements of a collection sequentially, it is best to use an enumerator<br />

interface. This provides the fastest method of walking through the collection.<br />

The reason for this is that each time an element is requested by index, internally<br />

an enumerator is used to locate the element. Hence, if the collection is looped<br />

over getting each element in turn, the time taken increases exponentially (y=cx b ).<br />

Enumerator use<br />

When requesting an enumerator interface from an object, the client has no idea<br />

how the object has implemented this interface. The object may create a new<br />

enumerator, or it may decide for efficiency to return a previously created enumerator.<br />

If a previous enumerator is passed to the client, the position of the<br />

element pointer will be at the last accessed element. To ensure that the enumerator<br />

is at the start of the collection, the client should reset the enumerator before<br />

use.<br />

Exception handling is language specific, and since<br />

COM is language neutral, exceptions are not<br />

supported.<br />

Error handling<br />

All methods of interfaces, in other words, methods callable from other objects,<br />

should handle internal errors and signify success or failure via an appropriate<br />

HRESULT. COM does not support passing exceptions out of interface method<br />

calls. COM supports the notion of a COM exception. A COM exception utilizes<br />

the COM error object by populating it with relevant information and returning an<br />

appropriate HRESULT to signify failure. Clients, on receiving the HRESULT,<br />

can then interrogate the COM Error object for contextual information about the<br />

error. Languages, such as Visual Basic, implement their own form of exception<br />

handling. For more information, see the specific section in this chapter for the<br />

language with which you are developing.<br />

Notification interfaces<br />

There are a number of interfaces in ArcObjects that have no methods. These are<br />

known as notification interfaces. Their purpose is to inform the application<br />

framework that the class that implements them supports a particular set of<br />

functionality. For instance, the application framework uses these interfaces to<br />

determine if a menu object is a root-level menu (IRootLevelMenu) or a context<br />

menu (IShortcutMenu).<br />

Client-side storage<br />

Some ArcObjects methods expect interface pointers to point to valid objects<br />

prior to making the method call. This is known as client storage since the client<br />

allocates the memory needed for the object before the method call. Suppose you<br />

have a polygon, and you want to obtain its bounding box. To do this, use the<br />

QueryEnvelope method on IPolygon. If you write the following code:<br />

Dim pEnv As IEnvelope<br />

pPolygon.QueryEnvelope pEnv<br />

you’ll get an error because the QueryEnvelope method expects you (the client) to<br />

create the Envelope. The method will modify the envelope you pass in and return<br />

the changed one back to you. The correct code follows:<br />

72 • <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!