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

While Wend constructs<br />

Avoid While … Wend constructs. Use the Do While … Loop or Do Until ... Loop<br />

instead because you can conditionally branch out of this construct.<br />

pFoos.Reset<br />

Set pFoo = pFoos.Next<br />

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

If (pFoo.Answer = "Done") Then Exit Loop<br />

Set pFoo = pFoos.Next<br />

Loop<br />

The VBVM was called the VB Runtime in earlier<br />

versions of the software.<br />

The Visual Basic Virtual Machine<br />

The Visual Basic Virtual Machine (VBVM) contains the intrinsic Visual Basic<br />

controls and services, such as starting and ending a Visual Basic application,<br />

required to successfully execute all Visual Basic developed code.<br />

The VBVM is packaged as a DLL that must be installed on any machine wanting<br />

to execute code written with Visual Basic, even if the code has been compiled to<br />

native code. If the dependencies of any Visual Basic compiled file are viewed,<br />

the file msvbvm60.dll is listed; this is the DLL housing the Virtual Machine.<br />

For more information on the services provided by the VBVM, see the sections<br />

‘Interacting with the IUnknown interface’ and ‘Working with HRESULTs’ in this<br />

chapter.<br />

Interacting with the IUnknown interface<br />

The section ‘The Microsoft Component Object Model’ earlier in this chapter<br />

contains a lengthy section on the IUnknown interface and how it forms the basis<br />

on which all of COM is built. Visual Basic hides this interface from developers<br />

and performs the required interactions (QueryInterface, AddRef, and Release function<br />

calls) on the developer’s behalf. It achieves this because of functionality<br />

contained within the VBVM. This simplifies development with COM for many<br />

developers, but to work successfully with ArcObjects, you must understand what<br />

the VBVM is doing.<br />

Visual Basic developers are accustomed to dimensioning variables as follows:<br />

Dim pColn as New Collection ' Create a new collection object.<br />

PColn.Add "Foo", "Bar" ' Add element to collection.<br />

It is worth considering what is happening at this point. From a quick inspection<br />

of the code, it appears that the first line creates a collection object and gives the<br />

developer a handle on that object in the form of pColn. The developer then calls a<br />

method on the object Add. Earlier in the chapter you learned that objects talk via<br />

their interfaces, never through a direct handle on the object itself. Remember,<br />

objects expose their services via their interfaces. If this is true, something isn’t<br />

adding up.<br />

What is actually happening is some “VB magic” performed by the VBVM and<br />

some trickery by the Visual Basic Editor (VBE) in the way that it presents objects<br />

and interfaces. The first line of code instantiates an instance of the collection<br />

class, then assigns the default interface for that object, _Collection, to the variable<br />

pColn. It is this interface, _Collection, that has the methods defined on it. Visual<br />

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