add field to an IFeatureClass

3559
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
HectorSanchez_Molinero
New Contributor

' Here it gives mistake

' Add the field.
            featureClass2.AddField(fields)


I have arranged it this way:

' Add the field.
            featureClass2.AddField(fieldsEdit.Field(0)) 


But on having tried to put a Alias does not it take it,

textFieldEdit.AliasName_2 = "Edificio_Afectado"

why it can be?
0 Kudos
HectorSanchez_Molinero
New Contributor
and as put then information in this new field??
0 Kudos
DorisRiedl
New Contributor
Does your FeatureClass already exist (does it have the necessary fields)?

And what kind of mistake did you get?

regards
Doris
0 Kudos
HectorSanchez_Molinero
New Contributor
The mistake in the creation of the fields already is corrected.

I answer the featureclass of the existing one and I him add the necessary fields.

The problem is that it does not take the Alias of the new fields:

                short2FieldEdit.AliasName_2 = "Tipo_max_min"
                short2FieldEdit.Name_2 = "Tipo_punto"

I do not know like add the new information in the created fields.

Thanks a lot Doris.

Regards, Héctor.
0 Kudos
FengZhang2
Occasional Contributor
In order to modify a field's Alias name, you can use the IClassSchemaEdit::AlterFieldAliasName method.

featureClass2.AddField(fields)

Dim classSchemaEdit As ESRI.ArcGIS.Geodatabase.IClassSchemaEdit3
classSchemaEdit = DirectCast(featureClass2, ESRI.ArcGIS.Geodatabase.IClassSchemaEdit3)

Dim schemaLock As ESRI.ArcGIS.Geodatabase.ISchemaLock
schemaLock = DirectCast(featureClass2, ESRI.ArcGIS.Geodatabase.ISchemaLock)
schemaLock.ChangeSchemaLock(ESRI.ArcGIS.Geodatabase.esriSchemaLock.esriExclusiveSchemaLock)
classSchemaEdit.AlterFieldAliasName("Tipo_punto", "Tipo_max_min")
schemaLock.ChangeSchemaLock(ESRI.ArcGIS.Geodatabase.esriSchemaLock.esriSharedSchemaLock)


The mistake in the creation of the fields already is corrected.

I answer the featureclass of the existing one and I him add the necessary fields.

The problem is that it does not take the Alias of the new fields:

                short2FieldEdit.AliasName_2 = "Tipo_max_min"
                short2FieldEdit.Name_2 = "Tipo_punto"

I do not know like add the new information in the created fields.

Thanks a lot Doris.

Regards, Héctor.
0 Kudos
HectorSanchez_Molinero
New Contributor
Thanks a lot feng.zhang.esri.

It continues without me working. On having come to the new code it gives the following mistake:

"It is not possible to turn the object COM of the type ' System. __ ComObject ' to the type of interface ' ESRI.ArcGIS.Geodatabase. IClassSchemaEdit3 '. A mistake of operation happened due to the fact that the so called QueryInterface in the component COM for the interface with IID ' {4B35F814-0417-47E3-8DFC-CAD58746693B} ' generated the following mistake: not compatible Interface (HRESULT's Exception: 0x80004002 (E_NOINTERFACE))."

Also I have tried with CType instead of DirectCast and neither it works.

regards.
0 Kudos
FengZhang2
Occasional Contributor
Please try to replace the following two lines:

Dim classSchemaEdit As ESRI.ArcGIS.Geodatabase.IClassSchemaEdit3
classSchemaEdit = DirectCast(featureClass2, ESRI.ArcGIS.Geodatabase.IClassSchemaEdit3)

with
Dim classSchemaEdit As IClassSchemaEdit3 = TryCast(featureClass2, IClassSchemaEdit3)


Thanks a lot feng.zhang.esri.

It continues without me working. On having come to the new code it gives the following mistake:

"It is not possible to turn the object COM of the type ' System. __ ComObject ' to the type of interface ' ESRI.ArcGIS.Geodatabase. IClassSchemaEdit3 '. A mistake of operation happened due to the fact that the so called QueryInterface in the component COM for the interface with IID ' {4B35F814-0417-47E3-8DFC-CAD58746693B} ' generated the following mistake: not compatible Interface (HRESULT's Exception: 0x80004002 (E_NOINTERFACE))."

Also I have tried with CType instead of DirectCast and neither it works.

regards.
0 Kudos
HectorSanchez_Molinero
New Contributor
Please try to replace the following two lines:

Dim classSchemaEdit As ESRI.ArcGIS.Geodatabase.IClassSchemaEdit3
classSchemaEdit = DirectCast(featureClass2, ESRI.ArcGIS.Geodatabase.IClassSchemaEdit3)

with
Dim classSchemaEdit As IClassSchemaEdit3 = TryCast(featureClass2, IClassSchemaEdit3)


Hi, thanks feng.zhang.

I have just modified the code for:

                Dim classSchemaEdit As IClassSchemaEdit3 = TryCast(featureClass2, IClassSchemaEdit3)

                Dim schemaLock As ESRI.ArcGIS.Geodatabase.ISchemaLock
                schemaLock = TryCast(featureClass2, ESRI.ArcGIS.Geodatabase.ISchemaLock)
                schemaLock.ChangeSchemaLock(ESRI.ArcGIS.Geodatabase.esriSchemaLock.esriExclusiveSchemaLock)
              
                classSchemaEdit.AlterFieldAliasName("Tipo_punto", "Tipo_max_min")
                schemaLock.ChangeSchemaLock(ESRI.ArcGIS.Geodatabase.esriSchemaLock.esriSharedSchemaLock)


it continues giving a mistake here, in this line:

classSchemaEdit.AlterFieldAliasName("Tipo_punto", "Tipo_max_min")

I have tried to change DirectCast for CType and TryCast.



the mistake it is:

{"Referencia a objeto no establecida como instancia de un objeto."}
{ " Reference to object not established as instance of an object. "}


what can fail?
0 Kudos
sreekarmasani
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);
0 Kudos