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 />

Iterating through a collection<br />

In your work with ArcObjects, you’ll discover that, in many cases, you’ll be<br />

working with collections. You can iterate through these collections with an<br />

enumerator. An enumerator is an interface that provides methods for traversing a<br />

list of elements. Enumerator interfaces typically begin with IEnum and have two<br />

methods: Next and Reset. Next returns the next element in the set and advances<br />

the internal pointer, and Reset resets the internal pointer to the beginning.<br />

Here is some VB code that loops through the selected features (IEnumFeature) in a<br />

map control.<br />

Dim pEnumFeat As IEnumFeature<br />

Dim pFeat As IFeature<br />

Set pEnumFeat = MapControl1.Map.FeatureSelection<br />

Set pFeat = pEnumFeat.Next<br />

Do While (Not pFeat Is Nothing)<br />

Debug.Print pFeat.Value(pFeat.Fields.FindField("state_name"))<br />

Set pFeat = pEnumFeat.Next<br />

Loop<br />

Some collection objects, the Visual Basic Collection being one, implement a<br />

special interface called _NewEnum. This interface, because of the _ prefix, is<br />

hidden, but Visual Basic developers can still use it to simplify iterating through a<br />

collection. The Visual Basic For Each construct works with this interface to<br />

perform the Reset and Next steps through a collection.<br />

Dim pColn as Collection<br />

Set pColn = GetCollection()' Collection returned from some function<br />

Dim thing as Variant ' VB uses methods on _NewEnum to step through<br />

For Each thing in pColn ' an enumerator.<br />

MsgBox Cstr(thing)<br />

Next<br />

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

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

Saved successfully!

Ooh no, something went wrong!