//*****Add a Field
IField2 pField = new Field() as IField2;
IFieldEdit2 pFieldEdit = pField as IFieldEdit2;
pFieldEdit.Name_2 = "BldCoding2";
pFieldEdit.AliasName_2 = "BldCoding2";
//pFieldEdit.Length_2 = 3;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldEdit.DefaultValue_2 = "1";
fcBiuding.AddField(pField);
//*****Then get the index of field
IEnumFeature pEnumFeature = ConstDef.m_Editor.EditSelection;
IFeature pBuildingFeature = null;
IFeatureCursor pFeaCur = null;
while ((pBuildingFeature = pEnumFeature.Next()) != null)
{
int i = pBuildingFeature.Fields.FindField("BldCoding2");// here is Error ,i=-1
}
public void AddFieldToFeatureClass(IFeatureClass featureClass, IField field)
{
ISchemaLock schemaLock = (ISchemaLock)featureClass;
try
{
// A try block is necessary, as an exclusive lock may not be available.
schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
// Add the field.
featureClass.AddField(field);
}
catch (Exception exc)
{
// Handle this in a way appropriate to your application.
Console.WriteLine(exc.Message);
}
finally
{
// Set the lock to shared, whether or not an error occurred.
schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
}
}