Subscribe Now: Feed Icon

Saturday, June 4, 2011

ArcObjects: Getting Started

This post is the second post in my ArcObjects series.

I have been an ESRI developer for the past ~5 years and in that time the biggest hurdle of all was the fact that ESRI uses COM objects. I don’t know how many times I got a COM Exception but they numbered at least in the hundreds if not the thousands (many of those could only be fixed by trial and error since the internet didn’t have the answer).

COM objects for those who don't know are not managed .NET code they are classes that are accessed through their interfaces, for example the Feature class implements IFeature, IRow and IValidate (at least according to the literature) but actually Feature implements only IRow:

Feature-object-browser

The class that actually implements IRow and IValidate is FeatureClass_2:

FeatureClass_2-object-browser

But don't use it!
Why not?

Because it's name suggest that in the next version ESRI will call it FeatureClass_3.
For this reason you shouldn't work directly with the Classes in ArcObjects  - use the interfaces instead.

 

Another thing that ESRI just loves is adding interfaces. A good example for this is PolylineClass:

PolylineClass-object-browser

This are only some of it's interfaces as you can see ESRI (and probably most COM modules) like to add interfaces with each version, for that reason we have six IGeometry interfaces which inherit from each other:

igeometry-object-browser

Meaning that you can use IGeometry5 and get all the methods from IGeometry.

This is not a rule though if you look at ITopologicalOperator6 you will find out that it doesn't inherit or at all connected to ITopologicalOperator:

ITopologicalOperator-object-browser

So be careful and use the minimum interface that you may need.


Because we are using COM objects and COM is a resource we should dispose each object after we are done with it, there are a few ways of doing this:

1. The manual way:

  1. System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor);

You release the COM object when you are done using it.

2. The managed way:

  1. using (var comReleaser = new ComReleaser())
  2. {
  3.     // Create insert feature cursor using buffering.
  4.     var featureCursor = featureClass.Insert(DefaultUseBufferingInInsert);
  5.     comReleaser.ManageLifetime(featureCursor);
  6.  
  7.     //...Use the cursor in some way
  8. }

At the end of the Using scope the resource is released.

I personally prefer the second way, but to each his own…

 

Next: ArcObjects: Extending the Framework

References:

Introduction to COM for ArcObjects developers

Releasing COM references

ArcObjects Resources

 

Keywords: ArcObjects, ComReleaser

IceRocket Tags: ,