add field to an IFeatureClass

3613
13
07-02-2012 11:50 PM
HectorSanchez_Molinero
New Contributor
Hello, I am beginning to programs with vb.net and c# and of arcobjects do not belong almost anything.

Can someone say to me how add a field (text or integer) to an object of type IFeatureClass and as put then information in him??

I try it this way:

' Create a new fields collection.
            Dim fields As IFields = New FieldsClass()

            ' Cast to IFieldsEdit to modify the properties of the fields collection.
            Dim fieldsEdit As IFieldsEdit = CType(fields, IFieldsEdit)

            ' Set the number of fields the collection will contain.
            fieldsEdit.FieldCount_2 = 1

            ' Create the text field.
            Dim textField As IField = New FieldClass()
            Dim textFieldEdit As IFieldEdit = CType(textField, IFieldEdit)
            textFieldEdit.Length_2 = 30 ' Only string fields require that you set the length.
            textFieldEdit.Name_2 = "Edificio_Afectado"
            textFieldEdit.Type_2 = esriFieldType.esriFieldTypeString

            ' Add the new field to the fields collection.
            fieldsEdit.Field_2(0) = textField


' Here it gives mistake

' Add the field.
            featureClass2.AddField(fields)



Thank you very much. Bye.
0 Kudos
13 Replies
MichaelRobb
Occasional Contributor III
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
0 Kudos
HectorSanchez_Molinero
New Contributor
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);


Hello, it is a good solution but already I have the prepared code of another form.

Thank you very much of all forms, for near times I will use it.
0 Kudos
HectorSanchez_Molinero
New Contributor
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


Thank you. My code is similar to his one (I add the characteristics of the fields of the same form) but it does not add the alias and I continue without knowing why (All the characteristics work except the alias. It does not give mistake simply does not add it).

A greeting
0 Kudos
NeilClemmons
Regular Contributor III
What type of feature class is it?  Not all feature classes support field aliases.  For example, if your feature class is a shapefile then you can't set an alias on a field because the shapefile has no way of storing it.  As far as I know, only geodatabase feature classes support field aliases.
0 Kudos