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

Bitwise operators<br />

Since And, Or, and Not are bitwise operators, ensure that all conditions using<br />

them test only for Boolean values, unless, of course, bitwise semantics are what is<br />

intended.<br />

If (Not pFoo Is Nothing) Then<br />

' Valid Foo do something with it<br />

End If<br />

Type suffixes<br />

Refrain from using type suffixes on variables or function names, such as myString$<br />

or Right$(myString), unless they are needed to distinguish 16-bit from 32-bit numbers.<br />

Ambiguous type matching<br />

For ambiguous type matching, use explicit conversion operators, such as CSng,<br />

CDbl, and CStr, instead of relying on VB to pick which one will be used.<br />

Simple image display<br />

Use an ImageControl rather than a PictureBox for simple image display. It is much<br />

more efficient.<br />

Recovery<br />

Statement<br />

Exit Sub<br />

Raise<br />

Resume<br />

Resume<br />

Next<br />

Frequency<br />

usually<br />

often<br />

rarely<br />

very rarely<br />

Meaning<br />

Function failed, pass control<br />

back to caller<br />

Raise a new error code in<br />

the caller's scope<br />

Error condition removed,<br />

reattempt offending<br />

statement<br />

Ignore error and continue<br />

with next statement<br />

Error handling<br />

Always use On Error to ensure fault-tolerant code. For each function that does<br />

error checking, use On Error to jump to a single error handler for the routine that<br />

deals with all exceptional conditions that are likely to be encountered. After the<br />

error handler processes the error—usually by displaying a message—it should<br />

proceed by issuing one of the recovery statements shown on the table to the left.<br />

Error handling in Visual Basic is not the same as general error handling in COM<br />

(see the section ‘Working with HRESULTs’ in this chapter).<br />

Recovery statements issued by error handlers<br />

Event functions<br />

Refrain from placing more than a few lines of code in event functions to prevent<br />

highly fractured and unorganized code. Event functions should simply dispatch to<br />

reusable functions elsewhere.<br />

Memory management<br />

To ensure efficient use of memory resources, the following points should be<br />

considered:<br />

• Unload forms regularly. Do not keep many forms loaded but invisible since<br />

this consumes system resources.<br />

• Be aware that referencing a form-scoped variable causes the form to be<br />

loaded.<br />

• Set unused objects to Nothing to free up their memory.<br />

• Make use of Class_Initialize and Class_Terminate to allocate and destroy<br />

resources.<br />

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

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

Saved successfully!

Ooh no, something went wrong!