[GIS] ArcObjects: modifying feature field value

arcgis-10.0arcmaparcobjectscenterprise-geodatabase

I'm developing an ArcMap extension and looking for a way to change feature attributes knowing its feature class and OID (and, presumably, everything else that there is in IFeature). All layers use ArcSDE datasets.

What's the right way to do it? Should I change the data in database? It feels like I shouldn't but I can't figure out how to change feature fields values using ArcObjects. The documentation for ArcObjects looks incomplete and awkward to use.

Background info: the extension provides a custom tool that processes selected feature and changes one of its attributes. Now, feature processing involves calling a remote service so it's an asynchronous operation and I should place all my updating logic in the callback method. The tool should work with any of the project layers all of which are ArcSDE datatsets. That's basically the requirements.

If anyone can help me with an advice or a working sample, I would be really grateful. Thanks in advance.

Best Answer

Below are a couple of extension methods for IFeature/IRow that show the actual setting of a value. You'll want to check out cursors and row/feature buffers if you need better performance: http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/0001000002rs000000.htm

the extension methods would need to be called when you are already in an edit operation (editor.StartOperation()/editor.StopOperation()): http://resources.esri.com/help/9.3/ArcGISDesktop/dotnet/abbd6e92-04f8-4957-a205-a53773fbf023.htm

    /// <summary>
    /// Sets the value on IFeature
    /// </summary>
    /// <param name="feature">The feature with the field to be set</param>
    /// <param name="fieldName">Name of the field.</param>
    /// <param name="value">The new field value</param>
    /// <param name="ignoreMissingFields">if set to <c>true</c> [ignore missing fields].</param>
    public static void SetValue(this IFeature feature, string fieldName, object value, bool ignoreMissingFields)
    {
        (feature as IRow).SetValue(fieldName, value, ignoreMissingFields);
    }

    /// <summary>
    /// Sets the value on IRow
    /// </summary>
    /// <param name="row">The row with the field to update</param>
    /// <param name="fieldName">Name of the field.</param>
    /// <param name="value">The new field value</param>
    /// <param name="ignoreMissingFields">if set to <c>true</c> [ignore missing fields].</param>
    public static void SetValue(this IRow row, string fieldName, object value, bool ignoreMissingFields)
    {
        int index = row.Fields.FindField(fieldName);

        if (index != -1)
        {
            if (row.Fields.get_Field(index).Editable)
            {
                row.set_Value(index, value);
            }
        }
        else
        {
            if (!ignoreMissingFields)
            {
                throw new System.ArgumentException("Field '{0}' Not Found.".FormatString(fieldName));
            }
        }
    }