ArcObject for reading a record value for a particular field

5293
2
08-16-2011 11:41 AM
JulietMora
New Contributor
I am writing a tool to read and compare two values (from two separate records) in a particular field. I cant seem to determine what ArcObject I need in order to read the value within the field. I will be reading a feature class within a gdb, not editing records... this tool is simply for record comparisons. Any help is greatly appriciated.. thanks.
0 Kudos
2 Replies
JeffreyHamblin
New Contributor III
Hello Juliet,

Below is a very rudimentary way of comparing the value of one field in two selected features.
You could call this method from a button click in a C# Add-In. If you are working in VBA or VB.NET then it will be a little different, but the interfaces used will be the same. Of course, replace the "YOUR_FIELDNAME" in the code with the actual field name.

public void PerformTest()
{
    // Get the selection in TOC.
    object item = ArcMap.Document.SelectedItem;

    // Make sure it is a feature layer
    if (item is IFeatureLayer)
    {
        IFeatureLayer featureLayer = item as IFeatureLayer;
        IFeatureSelection featureSelection = featureLayer as IFeatureSelection;
        ISelectionSet selectionSet = featureSelection.SelectionSet;

        // Make sure exactly 2 features are selected
        if (selectionSet.Count == 2)
        {
            // Get a cursor
            ICursor cursor = null;
            selectionSet.Search(null, true, out cursor);
            

            // Get the index of the subject field
            int fieldIdx = featureLayer.FeatureClass.FindField("YOUR_FIELDNAME");

            // Typically you would cycle the rows of the cursor in a loop, but since
            // we know we have exactly 2 rows to compare, just grab them.

            // Set current row to first selected feature and get the field's value
            IRow row = cursor.NextRow();
            string value1 = row.get_Value(fieldIdx).ToString();

            // Set current row to second selected feature and get the field's value
            row = cursor.NextRow();
            string value2 = row.get_Value(fieldIdx).ToString();

            // Display the two values
            MessageBox.Show("Feature 1: " + value1 +
                "\nFeature 2: " + value2);
        }
        else
        {
            MessageBox.Show("Please select exactly 2 features.");
        }

    }
    else
    {
        MessageBox.Show("Please select a feature layer.");
    }


}
0 Kudos
Ariharan
New Contributor
Hai Jeff,
             Am also in the similar process. Can you please give me some sample to select single and group of features from a specified layer on mouse click at runtime.

In this above mentioned code all working fine, But the selectionset count is 0. How can i select feature(Probably polygon) from layer? I need to select only one feature at a time.

Do you have any sample regarding this?
0 Kudos