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

Private Sub VBMagic()<br />

' Dim a variable to the IUnknown interface on the simple object.<br />

Dim pUnk As IUnknown<br />

See the Visual Basic Magic sample on the disk<br />

for this code. You are encouraged to run the<br />

sample and use the code. This object also uses<br />

an ATL C++ project to define the SimpleObject<br />

and its interfaces; you are encouraged to look at<br />

this code to learn a simple implementation of a<br />

C++ ATL object.<br />

' Co Create simpleobject asking for the IUnknown interface.<br />

Set pUnk = New SimpleObject 'refCount = 1<br />

' QI for a useful interface.<br />

' Define the interface.<br />

Dim pMagic As ISimpleObject<br />

' Perform the QI operation.<br />

Set pMagic = punk 'refCount = 2<br />

' Dim another variable to hold another interface on the object.<br />

Dim pMagic2 As IAnotherInterface<br />

' QI for that interface.<br />

Set pMagic2 = pMagic 'refCount = 3<br />

' Release the interface pointer.<br />

Set pMagic2 = Nothing 'refCount = 2<br />

' Release the interface.<br />

Set pMagic = Nothing 'refCount = 1<br />

' Now reuse the pUnk variable - what will VB do for this?<br />

Set pUnk = New SimpleObject 'refCount = 1, then 0, then 1<br />

' Let the interface variable go out of scope and let VB tidy up.<br />

End Sub 'refCount = 0<br />

Often interfaces have properties that are actually pointers to other interfaces.<br />

Visual Basic allows you to access these properties in a shorthand fashion by<br />

chaining interfaces together. For instance, assume that you have a pointer to the<br />

IFoo interface, and that interface has a property called Gak that is an IGak interface<br />

with the method DoSomething. You have a choice on how to access the<br />

DoSomething method. The first method is the long-handed way.<br />

Dim pGak as IGak<br />

Set pGak = pFoo ' Assign IGak interface to local variable.<br />

pGak.DoSomething ' Call method on IGak interface.<br />

Alternatively, you can chain the interfaces and accomplish the same thing on one<br />

line of code.<br />

pFoo.Gak.DoSomething ' Call method on IGak interface.<br />

When looking at the sample code, you will see both methods. Normally, the<br />

former method is used on the simpler samples, as it explicitly tells you what<br />

interfaces are being worked with. More complex samples use the shorthand<br />

method.<br />

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