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.

.NET APPLICATION PROGRAMMING INTERFACE<br />

that variable can be used to access the IPoint::PutCoords method from this class<br />

interface.<br />

[VB.NET]<br />

Dim thePt As ESRI.<strong>ArcGIS</strong>.Geometry.Point = New ESRI.<strong>ArcGIS</strong>.Geometry.Point()<br />

thePt.PutCoords(10,8)<br />

[C#]<br />

ESRI.<strong>ArcGIS</strong>.Geometry.Point thePt = newESRI.<strong>ArcGIS</strong>.Geometry.Point();<br />

thePt.PutCoords(10,8);<br />

The inherited interface of a class interface is not guaranteed to remain the same<br />

between versions of <strong>ArcGIS</strong> and, therefore, it is recommended that you avoid<br />

using the above syntax.<br />

You can view these types in the VB .NET Object Browser. Notice that using<br />

Visual Basic .NET, PointClass is not shown by default but can be made visible by<br />

selecting the Show Hidden Members option. In the C# Object Browser, you can<br />

see more clearly the class interface Point and its inherited interface IPoint and the<br />

class PointClass.<br />

.NET PROGRAMMING TECHNIQUES AND CONSIDERATIONS<br />

This section contains several programming tips and techniques to help developers<br />

who are moving to .NET.<br />

Casting between interfaces (QueryInterface)<br />

.NET uses casting to jump from one interface to another interface on the same<br />

class. In COM this is called QueryInterface. VB.NET and C# cast differently.<br />

VB.NET<br />

There are two types of casts, implicit and explicit. Implicit casts require no<br />

additional syntax, whereas explicit casts require cast operators.<br />

geometry = point<br />

' Implicit cast<br />

geometry = CType(point, IGeometry) ' Explicit cast<br />

When casting between interfaces, it’s perfectly acceptable to use implicit casts<br />

because there is no chance of data loss as there is when casting between numeric<br />

types. However, when casts fail, an exception (System.InvalidCastException) is<br />

thrown; to avoid handling unnecessary exceptions, it’s best to test if the object<br />

implements both interfaces beforehand. The recommended technique is to use the<br />

TypeOf keyword, which is a comparison clause that tests whether an object is<br />

derived from or implements a particular type, such as an interface. The example<br />

below performs an implicit conversion from an IPoint to an IGeometry only if at<br />

runtime it is determined that the Point class implements IGeometry.<br />

Dim point As New PointClass<br />

Dim geometry As IGeometry<br />

If (TypeOf point Is IGeometry) Then<br />

geometry = point<br />

End If<br />

If you prefer using the Option Strict On statement to restrict implicit conversions,<br />

use the CType function to make the cast explicit. The example below adds<br />

an explicit cast to the code sample above.<br />

Dim point As New PointClass<br />

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