Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It pains me to ask such a basic question, but after several hours (not exaggerating) of trying to figure this out and failing, I come to

What I am trying to do is pass a selected feature and/or layer to a given method. In this example, it is to add a field to the selected layer in the ToC. The snippet calls for a feature class that is a member of the IFeatureClass interface (if my vocabulary is off, please correct me):

public void AddFieldToFeatureClass(IFeatureClass featureClass, IField field)

I create an IField easily enough:

IField statusField = new FieldClass();

But what is the preferred, cleanest, or most effective method to pass an IFeatureClass that is the currently selected layer in the TOC?

share|improve this question
up vote 6 down vote accepted

This works for me :

var fLayer = ArcMap.Document.CurrentContentsView.SelectedItem as IFeatureLayer;
if (fLayer != null)
    Debug.Print("got featurelayer");
    Debug.Print("no featurelayer selected (or maybe more than one is)");
share|improve this answer
I'm always being told that there is no Feature Layer selected... do I need to set up the mxDocument and ActiveView first? – Nathanus May 18 '11 at 15:47
When you look at the TOC in arcmap, is there a featurelayer selected? – Kirk Kuykendall May 18 '11 at 16:06
The method I'm using actually calls for a feature class, so I changed it to as IFeatureClass. Would that cause a problem? – Nathanus May 18 '11 at 16:10
Yes, the SelectedItem will return a featurelayer - not a featureclass. To get the featureclass use IFeatureLayer.Featureclass. – Kirk Kuykendall May 18 '11 at 16:16
I finally got a handle on it. Thanks a lot-- your answer put me on the right path. – Nathanus May 19 '11 at 14:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.