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

That way, if there happens to be another IPoint referenced in your project, there<br />

won’t be any ambiguity as to which one you are referring to.<br />

Coclass is an abbreviation of component object<br />

class.<br />

A QI is required since the default interface of<br />

the object is IUnknown. Since the pPt variable<br />

was declared as type IPoint, the default<br />

IUnknown interface was QI’d for the IPoint<br />

interface.<br />

This is the compilation error message shown<br />

when a method or property is not found on an<br />

interface.<br />

The second line of the fragment creates an instance of the object or coclass, then<br />

performs a QI operation for the IPoint interface that it assigns to pPt.<br />

With a name for the coclass as common as Point, you may want to precede the<br />

coclass name with the library name, for example:<br />

Set pPt = New esriGeometry.Point<br />

The last line of the code fragment invokes the PutCoords method. If a method<br />

can’t be located on the interface, an error will be shown at compile time.<br />

Working with HRESULTs<br />

So far, you have seen that all COM methods signify success or failure via an<br />

HRESULT that is returned from the method; no exceptions are raised outside the<br />

interface. You have also learned that Visual Basic raises exceptions when errors<br />

are encountered. In Visual Basic, HRESULTs are never returned from method<br />

calls, and to confuse you further when errors do occur, Visual Basic throws an<br />

exception. How can this be? The answer lies with the Visual Basic Virtual Machine.<br />

It is the VBVM that receives the HRESULT; if this is anything other than<br />

S_OK, the VBVM throws the exception. If it was able to retrieve any worthwhile<br />

error information from the COM error object, it populates the Visual Basic<br />

Err object with that information. In this way, the VBVM handles all HRESULTs<br />

returned from the client.<br />

When implementing interfaces in Visual Basic, it is good coding practice to raise<br />

an HRESULT error to inform the caller that an error has occurred. Normally, this<br />

is done when a method has not been implemented.<br />

' Defined in module<br />

Const E_NOTIMPL = &H80004001 ' Constant that represents HRESULT<br />

' Added to any method not implemented<br />

On Error GoTo 0<br />

Err.Raise E_NOTIMPL<br />

You must also write code to handle the possibility that an HRESULT other than<br />

S_OK is returned. When this happens, an error handler should be called and the<br />

error dealt with. This may mean simply notifying the user, or it may mean automatically<br />

dealing with the error and continuing with the function. The choice<br />

depends on the circumstances. Below is a simple error handler that will catch any<br />

error that occurs within the function and report it to the user. Note the use of<br />

the Err object to provide the user with some description of the error.<br />

Private Sub Test()<br />

On Error GoTo ErrorHandler<br />

' Do something here.<br />

Exit Sub ' Must exit sub here before error handler<br />

ErrorHandler:<br />

Msgbox "Error In Application – Description " & Err.Description<br />

End Sub<br />

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