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.

The following code works for retrieving values within a particular field(Name1), but I want to retrieve mulitple fields. Am I to create multiple variables for each field?

IMxDocument pMxDocument = ArcMap.Application.Document as IMxDocument;
        IMap pMap = pMxDocument.FocusMap;
        ILayer pLayer = pMap.get_Layer(0);
        IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer;
        IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;

        ITable pTable = (ITable)pFeatureClass;
        ICursor pCursor = pTable.Search(null, false);
        int fldIndex = pFeatureClass.Fields.FindField("Name1");
        IRow pRow = pCursor.NextRow();
        while (pRow != null)
        {
            MessageBox.Show(pRow.get_Value(fldIndex).ToString());
            pRow = pCursor.NextRow();

        }

Edit: What I'm trying to do is get the selected features of a feature class, get the values for fields like Name, address, city, etc and then pass those values onto a document. I thought I would give some background to what I am doing since I may be on the wrong track to start off with.

share|improve this question
add comment

2 Answers

Here are two examples of getting values from the selected features.

This one lists all the values for the feature

IMxDocument pMxDocument = ArcMap.Application.Document as IMxDocument;
IMap pMap = pMxDocument.FocusMap;
ILayer pLayer = pMap.get_Layer(0);
IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFields pFields = (IFields)pFeatureClass;
ITable pTable = (ITable)pFeatureClass;
ICursor pCursor = pTable.Search(null, false);
IRow pRow = pCursor.NextRow();
while (pRow != null)
{
    string.IsNullOrEmpty(output);
    for (int i = 0; i <= pFields.FieldCount - 1; i++) {
        output += pFields.Field(i).Name + ": " + pRow.Value(i).ToString + Constants.vbNewLine;
    }
    MessageBox.Show(pRow.get_Value(fldIndex).ToString());
    pRow = pCursor.NextRow();

}

This example lists just selected values of the feature

    IMxDocument pMxDocument = ArcMap.Application.Document as IMxDocument;
IMap pMap = pMxDocument.FocusMap;
ILayer pLayer = pMap.get_Layer(0);
IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFields pFields = (IFields)pFeatureClass;
ITable pTable = (ITable)pFeatureClass;
ICursor pCursor = pTable.Search(null, false);
string[] FieldNames = {"Name", "Address", "City"};
string output = null;
IRow pRow = pCursor.NextRow();
while (pRow != null)
{
    string.IsNullOrEmpty(output);
    foreach (string fieldName in FieldNames) {
        output += fieldName + ": " + pRow.Value(pFields.FindField(fieldName)).ToString + Constants.vbNewLine;
    }
    MessageBox.Show(output);
    pRow = pCursor.NextRow();

}
share|improve this answer
    
I think KFP needs selected features, not selected fields."What I'm trying to do is get the selected features of a feature class" –  Emi Jun 4 '13 at 3:16
add comment

I use following code snippet to get selected features from map for a specific feature layer:

public List<IFeature> GetSelectedFeatures(IFeatureLayer featureLayer)
{
    IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;
    var selectionSet = featureSelection.SelectionSet;
    IFeatureClass featureClass = featureLayer.FeatureClass;
    string shapeField = featureClass.ShapeFieldName;
    ISpatialFilter spatialFilter = new SpatialFilterClass();
    spatialFilter.GeometryField = shapeField;
    spatialFilter.set_OutputSpatialReference(shapeField, _map.SpatialReference);

    ICursor cursor;
    selectionSet.Search(spatialFilter, false, out cursor);
    var featureCursor = cursor as IFeatureCursor;
    var features = new List<IFeature>();

    IFeature feature;
    while ((feature = featureCursor.NextFeature()) != null)
        features.Add((feature));

    return features;
}

To know more about selection you can go through IFeatureSelection Interface.

And as kenbuja says, you can copy fields by iterating through rows.

share|improve this answer
add comment

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.