In this topic
About working with fields
Each table in a geodatabase has an ordered collection of fields (there is always at least one field in a table). The ordered collection behaves like a list, making it possible to access individual fields by their numbered positions (their indexes). To access a field set from a table, use the IClass.Fields property (inherited by ITable and IFeatureClass).
Field sets
A field set can also exist independently of a table, for example, when creating an index on a table, the fields that are included in the index must be defined using a field set. Many interfaces require or return a field set, which includes ICursor, IIndex, IFeatureClassDraw, IRowBuffer, ITableSort, and IValidate.
IFields interface
The IFields interface provides information about a field set and provides access to individual fields. The following code example finds any fields in a feature class that have an alias name distinct from their physical name:
[C#] public void DisplayDistinctFieldAliasNames(IFeatureClass featureClass)
{
// Get the Fields collection from the feature class.
IFields fields=featureClass.Fields;
IField field=null;
// On a zero based index, iterate through the fields in the collection.
for (int i=0; i < fields.FieldCount; i++)
{
// Get the field at the given index.
field=fields.get_Field(i);
if (field.Name != field.AliasName)
{
Console.WriteLine("{0} : {1}", field.Name, field.AliasName);
}
}
}
[VB.NET] Public Sub DisplayDistinctFieldAliasNames(ByVal featureClass As IFeatureClass)
' Get the Fields collection from the feature class.
Dim fields As IFields=featureClass.Fields
Dim field As IField=Nothing
' On a zero based index, iterate through the fields in the collection.
For i As Integer=0 To fields.FieldCount - 1
' Get the field at the given index.
field=fields.Field(i)
If field.Name <> field.AliasName Then
Console.WriteLine("{0} : {1}", field.Name, field.AliasName)
End If
Next i
End Sub
When using IFields.FindField, remember that there are equivalent methods on IClass and ICursor. Calling FindField on a table or cursor returns the same result as calling the method on the fields collection.
Developers working with SDE should be aware of certain field names that conflict with properties used internally by SDE for storing geometry, for example, LEN. Although not strictly prohibited, using these field names result in qualification of the field name, and should be avoided. For more details, and a complete list of field names that conflict with SDE properties, see IFields2.
The following code example shows how to get a reference to a specific field in a collection using the field's name:
[C#] public IField GetFieldByName(IFeatureClass featureClass, String fieldName)
{
// Find the index of the requested field.
int fieldIndex=featureClass.FindField(fieldName);
// Get the field from the feature class's fields collection.
IFields fields=featureClass.Fields;
IField field=fields.get_Field(fieldIndex);
return field;
}
[VB.NET] Public Function GetFieldByName(ByVal featureClass As IFeatureClass, ByVal fieldName As String) As IField
' Find the index of the requested field.
Dim fieldIndex As Integer=featureClass.FindField(fieldName);
' Get the field from the feature class's fields collection.
Dim fields As IFields=featureClass.Fields
Dim field As IField=fields.Field(fieldIndex)
Return field
End Function
IField interface
A field has many properties (the most obvious are the name and data type). The esriFieldType enumeration lists the available 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 type esriFieldTypeGeometry, the IField.GeometryDef property is used to return the geometry definition (as IGeometryDef).
Field objects do not provide access to all ArcInfo-based data types (coverages) and attributes. If access to coverage-specific characteristics is required, use the IArcInfoItem interface.
public void DisplayFieldProperties(IFeatureClass featureClass)
{
// Get the fields collection for the feature class.
IFields fields=featureClass.Fields;
IField field=null;
for (int i=0; i < fields.FieldCount; i++)
{
field=fields.get_Field(i);
// Get the standard field properties.
Console.WriteLine("Name: {0}", field.Name);
Console.WriteLine("Alias: {0}", field.AliasName);
Console.WriteLine("Type: {0}", field.Type);
Console.WriteLine("VarType: {0}", field.VarType);
Console.WriteLine("Default Value: {0}", field.DefaultValue);
Console.WriteLine("Length: {0}", field.Length);
Console.WriteLine("Precision: {0}", field.Precision);
Console.WriteLine("Scale: {0}", field.Scale);
Console.WriteLine("Is Editable: {0}", field.Editable);
Console.WriteLine("Is Nullable: {0}", field.IsNullable);
Console.WriteLine("Is Required: {0}", field.Required);
// Check if the field is the shape field.
if (field.Type == esriFieldType.esriFieldTypeGeometry)
{
IGeometryDef geometryDef=field.GeometryDef;
Console.WriteLine("Geometry Type: {0}", geometryDef.GeometryType);
Console.WriteLine("Has M Values: {0}", geometryDef.HasM);
Console.WriteLine("Has Z Values: {0}", geometryDef.HasZ);
}
// Insert an empty line for readability.
Console.WriteLine();
}
}
[VB.NET] Public Sub DisplayFieldProperties(ByVal featureClass As IFeatureClass)
' Get the fields collection for the feature class.
Dim fields As IFields=featureClass.Fields
Dim field As IField=Nothing
For i As Integer=0 To fields.FieldCount - 1
field=fields.Field(i)
' Get the standard field properties.
Console.WriteLine("Name: {0}", field.Name)
Console.WriteLine("Alias: {0}", field.AliasName)
Console.WriteLine("Type: {0}", field.Type)
Console.WriteLine("VarType: {0}", field.VarType)
Console.WriteLine("Default Value: {0}", field.DefaultValue)
Console.WriteLine("Length: {0}", field.Length)
Console.WriteLine("Precision: {0}", field.Precision)
Console.WriteLine("Scale: {0}", field.Scale)
Console.WriteLine("Is Editable: {0}", field.Editable)
Console.WriteLine("Is Nullable: {0}", field.IsNullable)
Console.WriteLine("Is Required: {0}", field.Required)
' Check if the field is the shape field.
If field.Type=esriFieldType.esriFieldTypeGeometry Then
Dim geometryDef As IGeometryDef=field.GeometryDef
Console.WriteLine("Geometry Type: {0}", geometryDef.GeometryType)
Console.WriteLine("Has M Values: {0}", geometryDef.HasM)
Console.WriteLine("Has Z Values: {0}", geometryDef.HasZ)
End If
' Insert an empty line for readability.
Console.WriteLine()
Next i
End Sub
See Also:
Creating fieldsTo 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):
Development licensing | Deployment licensing |
---|---|
ArcGIS Desktop Basic | ArcGIS Desktop Basic |
ArcGIS Desktop Standard | ArcGIS Desktop Standard |
ArcGIS Desktop Advanced | ArcGIS Desktop Advanced |
Engine Developer Kit | Engine |