Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 GIS.se

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

1 Answer 1

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");
else
    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
2  
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

 
discard

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.