[GIS] get valules for all fields from selected features

arcmaparcobjectscvisual studio

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.

Best Answer

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();

}