Sub AddFieldtoFeature(ByVal FeatureLayer As ESRI.ArcGIS.Carto.IFeatureLayer, ByVal FieldName As String, ByVal FieldType As ESRI.ArcGIS.Geodatabase.esriFieldType, ByVal scale As Integer, ByVal Precision As Integer, ByVal Length As Integer, ByVal IsNullable As Boolean, ByVal AliasName As String, ByVal DefaultValue As String)
Dim pMxdoc As ESRI.ArcGIS.ArcMapUI.IMxDocument
Dim pMap As ESRI.ArcGIS.Carto.IMap
Dim pApp As ESRI.ArcGIS.Framework.IApplication
pApp = m_application
pMxdoc = m_application.Document
pMap = pMxdoc.FocusMap ' the active map the button was pressed, use Approt if you want to connect to different map
Dim pFeatureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass
pFeatureClass = FeatureLayer.FeatureClass
Dim pField As ESRI.ArcGIS.Geodatabase.IFieldEdit2
pField = New ESRI.ArcGIS.Geodatabase.Field
Dim pCheckField As ESRI.ArcGIS.Geodatabase.IFields
Dim CheckFieldInt As Integer
Try
pCheckField = pFeatureClass.Fields
CheckFieldInt = pCheckField.FindField(FieldName)
If CheckFieldInt = -1 Then
' lets make sure we are OUT OF editing but do this so if all fields are created... we can stay in edit mode:
Dim pID As New ESRI.ArcGIS.esriSystem.UID
pID.Value = "esriEditor.Editor" ' PROG ID
'or the CLSID
'pID.Value = "{F8842F20-BB23-11D0-802B-0000F8037368}"
Dim pEditor As ESRI.ArcGIS.Editor.IEditor
pEditor = pApp.FindExtensionByCLSID(pID)
If pEditor.EditState = ESRI.ArcGIS.Editor.esriEditState.esriStateEditing Then ' if in edit mode...
Dim pUID As New ESRI.ArcGIS.esriSystem.UID
pUID.Value = "{59D2AFD1-9EA2-11D1-9165-0080C718DF97}" 'STOP EDITING, MUST HAVE {}
Dim pCommandBars As ESRI.ArcGIS.Framework.ICommandBars
Dim pCommandItem As ESRI.ArcGIS.Framework.ICommandItem
pCommandBars = pApp.Document.CommandBars
pCommandItem = pCommandBars.Find(pUID)
pCommandItem.Execute()
'start editing "{59D2AFD0-9EA2-11D1-9165-0080C718DF97}"
System.Threading.Thread.Sleep(100)
End If
pField.Name_2 = FieldName
pField.Type_2 = FieldType
'Select appropriately based on field type selected
If FieldType = esriFieldType.esriFieldTypeDouble Or FieldType = esriFieldType.esriFieldTypeInteger Or FieldType = esriFieldType.esriFieldTypeSmallInteger Then
pField.Scale_2 = scale
pField.Precision_2 = Precision
End If
If FieldType = esriFieldType.esriFieldTypeString Then
pField.Length_2 = Length
End If
If IsNullable <> Nothing Then
pField.IsNullable_2 = IsNullable
End If
If AliasName <> "" Then
pField.AliasName_2 = AliasName
End If
If DefaultValue <> "" Then
pField.DefaultValue_2 = DefaultValue
End If
pFeatureClass.AddField(pField)
End If
Catch ex As Exception
MessageBox.Show("Failed to create field" & vbNewLine & ex.Message & vbNewLine & "Error Details:" & vbNewLine & ex.StackTrace, "Create Feature Field", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
It is beeter to use geoProcessing tools instead of writing our own code if one is available in GPTools.
Geoprocessor gProcess = new Geoprocessor();
IVariantArray varArray = new VarArrayClass();
varArray.Add(@"...\MyPGDB.mdb\DatasetTwo\Line_2");
varArray.Add("MyNewField");
varArray.Add("TEXT");
gProcess.Execute("AddField", varArray, null);
I had written a function which is generic, allowing you to create various fields. Just call the AddFieldtoFeature in your main code block. It does check if the field already exists.
Yes, the GP works as well, but I prefer this way... full control.
You would just call by:
AddFieldtoFeature(pFeaturelayer, "FIELDNAME", esriFieldType.esriFieldTypeString, Nothing, Nothing, 20, Nothing, "ALIAS", Nothing)
**This works by passing the appropriate information, any info that is to be left out needs to be NOTHING , "" will not work.
Copy the entire code below and place somewhere in the Class. you will have to reference the pMxDoc yourself. (m_Application)
Sub AddFieldtoFeature(ByVal FeatureLayer As ESRI.ArcGIS.Carto.IFeatureLayer, ByVal FieldName As String, ByVal FieldType As ESRI.ArcGIS.Geodatabase.esriFieldType, ByVal scale As Integer, ByVal Precision As Integer, ByVal Length As Integer, ByVal IsNullable As Boolean, ByVal AliasName As String, ByVal DefaultValue As String) Dim pMxdoc As ESRI.ArcGIS.ArcMapUI.IMxDocument Dim pMap As ESRI.ArcGIS.Carto.IMap Dim pApp As ESRI.ArcGIS.Framework.IApplication pApp = m_application pMxdoc = m_application.Document pMap = pMxdoc.FocusMap ' the active map the button was pressed, use Approt if you want to connect to different map Dim pFeatureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass pFeatureClass = FeatureLayer.FeatureClass Dim pField As ESRI.ArcGIS.Geodatabase.IFieldEdit2 pField = New ESRI.ArcGIS.Geodatabase.Field Dim pCheckField As ESRI.ArcGIS.Geodatabase.IFields Dim CheckFieldInt As Integer Try pCheckField = pFeatureClass.Fields CheckFieldInt = pCheckField.FindField(FieldName) If CheckFieldInt = -1 Then ' lets make sure we are OUT OF editing but do this so if all fields are created... we can stay in edit mode: Dim pID As New ESRI.ArcGIS.esriSystem.UID pID.Value = "esriEditor.Editor" ' PROG ID 'or the CLSID 'pID.Value = "{F8842F20-BB23-11D0-802B-0000F8037368}" Dim pEditor As ESRI.ArcGIS.Editor.IEditor pEditor = pApp.FindExtensionByCLSID(pID) If pEditor.EditState = ESRI.ArcGIS.Editor.esriEditState.esriStateEditing Then ' if in edit mode... Dim pUID As New ESRI.ArcGIS.esriSystem.UID pUID.Value = "{59D2AFD1-9EA2-11D1-9165-0080C718DF97}" 'STOP EDITING, MUST HAVE {} Dim pCommandBars As ESRI.ArcGIS.Framework.ICommandBars Dim pCommandItem As ESRI.ArcGIS.Framework.ICommandItem pCommandBars = pApp.Document.CommandBars pCommandItem = pCommandBars.Find(pUID) pCommandItem.Execute() 'start editing "{59D2AFD0-9EA2-11D1-9165-0080C718DF97}" System.Threading.Thread.Sleep(100) End If pField.Name_2 = FieldName pField.Type_2 = FieldType 'Select appropriately based on field type selected If FieldType = esriFieldType.esriFieldTypeDouble Or FieldType = esriFieldType.esriFieldTypeInteger Or FieldType = esriFieldType.esriFieldTypeSmallInteger Then pField.Scale_2 = scale pField.Precision_2 = Precision End If If FieldType = esriFieldType.esriFieldTypeString Then pField.Length_2 = Length End If If IsNullable <> Nothing Then pField.IsNullable_2 = IsNullable End If If AliasName <> "" Then pField.AliasName_2 = AliasName End If If DefaultValue <> "" Then pField.DefaultValue_2 = DefaultValue End If pFeatureClass.AddField(pField) End If Catch ex As Exception MessageBox.Show("Failed to create field" & vbNewLine & ex.Message & vbNewLine & "Error Details:" & vbNewLine & ex.StackTrace, "Create Feature Field", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub