This document is archived and information here might be outdated.  Recommended version.


Listing fields (ArcObjects .NET 10.5 SDK)

Listing fields


About listing fields

The IGeoProcessor2 interface does not provide a method to directly access the fields of a feature class or table since fields can be listed by ArcObjects from the Geodatabase library conveniently. The IGPUtilities3 interface of the Geoprocessing library provides three methods to access fields of a GPValue object.

The following code example shows how to get the fields of a feature class with the Fields property and FindField method of the IFeatureClass interface, then use the Calculate Field geoprocessing tool to update the values of the NEW_YIELD field with the values from the MODEL_YIELD field:
[C#]
public void FieldsFromIFeatureClass(IGeoProcessor2 gp, IFeatureClass fc)
{
    IFields fields=fc.Fields;

    IVariantArray parameters=new VarArrayClass();
    object sev=null;

    int fieldIndex1=fields.FindField("NEW_YIELD");
    int fieldIndex2=fields.FindField("MODEL_YIELD");

    if ((fieldIndex1 >  - 1) && (fieldIndex2 >  - 1))
    {
        parameters.Add(fc);
        parameters.Add("NEW_YIELD");
        parameters.Add("!MODEL_YIELD!");
        parameters.Add("PYTHON_9.3");
        gp.Execute("CalculateField_management", parameters, null);
        Console.WriteLine(gp.GetMessages(ref sev));
    }
}
[VB.NET]
Public Sub testFieldsFromIFeatureClass(ByVal gp As IGeoProcessor2, ByVal fc As IFeatureClass)
    
    Dim fields As IFields=fc.Fields
    
    Dim parameters As IVariantArray=New VarArray
    Dim sev As Object=Nothing
    
    Dim fieldIndex1=fields.FindField("NEW_YIELD")
    Dim fieldIndex2=fields.FindField("MODEL_YIELD")
    
    If ((fieldIndex1 > -1) And (fieldIndex2 > -1)) Then
        parameters.Add(fc)
        parameters.Add("NEW_YIELD")
        parameters.Add("!MODEL_YIELD!")
        parameters.Add("PYTHON_9.3")
        
        gp.Execute("CalculateField_management", parameters, Nothing)
        Console.WriteLine(gp.GetMessages(sev))
        
    End If
    
End Sub
You can also use the methods on the IGPUtilities3 interface to work with Field objects if you have a GPValue object. The GetOutput method of an IGeoProcessorResult object returns an IGPValue object. Thus, you always get a GPValue object from the result of tool execution if the output is a feature class.






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):