How to work with fields


Summary
This document shows how to gain access to a Fields collection and individual fields contained within. The workflow for accessing the read-only properties of a field is also shown, including the associated domain and the geometry definition from a shape field.

In this topic


Working with fields

Each table in a database has an ordered collection of fields; there is always at least one field in a table. The ordered collection behaves like a list; therefore, it is possible to access individual fields by a numbered position (or index) in the list.

Fields collections

A Fields collection can also exist independently of a table; for example, when creating an index on a table, you need to define which fields take part in the index. Many interfaces either require or return a Fields collection; these include IClass (therefore, also IObjectClass and IFeatureClass), ICursor, IIndex, IFeatureClassDraw, IRowBuffer, ISimpleDataConverter, ITableSort, and IValidate.

IFields interface

The IFields interface provides information about a Fields collection and provides access to individual fields. The following code example finds any fields in a feature class that have a distinct alias name:
[Java]
static void printDistinctFieldAliasNames(IFeatureClass featureClass)throws Exception{
    // get the Fields collection from the feature class
    IFields fields = featureClass.getFields();
    IField field;
    // on a zero based index iterate through the fields in the collection
    for (int i = 0; i < fields.getFieldCount(); i++){
        // get the field at the given index
        field = fields.getField(i);
        if (!field.getName().equals(field.getAliasName())){
            System.out.println(field.getName() + ":" + field.getAliasName());
        }
    }
}
When using IFields.FindField, remember that there are equivalent methods on IClass and ICursor—shortcuts that save you from having to get the Fields collection.
The following code example shows how you can get a reference to a specific field in a collection using the field's name:
[Java]
static IField getFieldByName(IFeatureClass featureClass, String fieldName)throws
    Exception{
    // Get the feature class' fields collection.
    IFields fields = featureClass.getFields();
    // Find the index of the requested field, access it, and return it.
    int fieldIndex = fields.findField(fieldName);
    IField field = fields.getField(fieldIndex);
    return field;
}

IField interface

A field has many properties; the most obvious ones are the name and data types. The esriFieldType enumeration lists the possible data types.
The IField interface is used for read-only access to the field's properties.
In the following code example, the IField interface is used to print the properties of each field in a feature class. If the field is of the type of esriFieldType::esriFieldTypeGeometry, the IField.GeometryDef property is used to return the geometry definition (IGeometryDef).
Field objects do not provide access to all Arc/INFO-based data types (coverages) and attributes.  If access to coverage-specific characteristics is required, use the IArcInfoItem interface.
[Java]
static void displayFieldProperties(IFeatureClass featureClass)throws Exception{
    // Get the fields collection for the feature class.
    IFields fields = featureClass.getFields();
    IField field = null;
    for (int i = 0; i < fields.getFieldCount(); i++){
        field = fields.getField(i);
        // Get the standard field properties.
        System.out.println("Name: " + field.getName());
        System.out.println("Alias: " + field.getAliasName());
        System.out.println("Type: " + field.getType());
        System.out.println("VarType: " + field.getVarType());
        System.out.println("Default Value: " + field.getDefaultValue());
        System.out.println("Length: " + field.getLength());
        System.out.println("Precision: " + field.getPrecision());
        System.out.println("Scale: " + field.getScale());
        System.out.println("Is Editable: " + field.isEditable());
        System.out.println("Is Nullable: " + field.isNullable());
        System.out.println("Is Required: " + field.isRequired());
        // Check if the field is the shape field.
        if (field.getType() == esriFieldType.esriFieldTypeGeometry){
            IGeometryDef geometryDef = field.getGeometryDef();
            System.out.println("Geometry Type: " + geometryDef.getGeometryType());
            System.out.println("Has M Values: " + geometryDef.isHasM());
            System.out.println("Has Z Values: " + geometryDef.isHasZ());
        }
        // Insert an empty line for readability.
        System.out.println();
    }
}


See Also:

How to create new fields
How to display field properties for a feature class




Development licensingDeployment licensing
ArcGIS for Desktop BasicArcGIS for Desktop Basic
ArcGIS for Desktop StandardArcGIS for Desktop Standard
ArcGIS for Desktop AdvancedArcGIS for Desktop Advanced
Engine Developer KitEngine