Checking for existing fields before adding

680
3
Jump to solution
10-04-2012 10:40 AM
KevinYanuk
Occasional Contributor
Hello -

I am trying to check whether a field exists before attempting to create it.  The code I have so far is:

private void CreateField(IDataset d, IFeatureClass f, string name, string alias)         {             // IF FIELD DOES NOT EXIST, RETURN, ELSE, CREATE              IField field = new FieldClass();             IFieldEdit fe = (IFieldEdit)field;             fe.Name_2 = name;             fe.AliasName_2 = alias;             fe.Type_2 = esriFieldType.esriFieldTypeString;             fe.Length_2 = 20;             Invoke(new MethodInvoker(delegate { UpdateLabel.Text = "Creating field " + field.Name + "\nfor " + d.Name.Split('.')[1] + "... "; }));             f.AddField(field);         }



Thanks,

Kevin
0 Kudos
1 Solution

Accepted Solutions
KevinYanuk
Occasional Contributor
Use IFeatureClass::FindField to check if a field exists. If it returns -1, the field isn't in the feature class.

if (f.findfield(name) == -1)      return;


Call the FindField method on the feature class' Fields collection.  If it returns -1 then the field doesn't exist.



Excellent, thank you both!

View solution in original post

0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor
Use IFeatureClass::FindField to check if a field exists. If it returns -1, the field isn't in the feature class.

if (f.findfield(name) == -1)
     return;
0 Kudos
NeilClemmons
Regular Contributor III
Call the FindField method on the feature class' Fields collection.  If it returns -1 then the field doesn't exist.
0 Kudos
KevinYanuk
Occasional Contributor
Use IFeatureClass::FindField to check if a field exists. If it returns -1, the field isn't in the feature class.

if (f.findfield(name) == -1)      return;


Call the FindField method on the feature class' Fields collection.  If it returns -1 then the field doesn't exist.



Excellent, thank you both!
0 Kudos